I just needed to do the same in one of my projects.
The solution I applied:
At Storyboard, my collection view spans the entire screen with Auto-Layout restrictions.
In view mode DidLoad:
collectionView.contentInset = UIEdgeInsets(top: 0, left: 0, bottom: 0, right: 0)
I set collectionView delegate for myself, and also compatible with
UICollectionViewDelegateFlowLayout.
, , UIEdgeInsets
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, insetForSectionAt section: Int) -> UIEdgeInsets {
return UIEdgeInsets(top: 15, left: 20, bottom: 15, right: 20)
}
.
, .

, .