As you mentioned here
collectionView.contentInset = UIEdgeInsets(top: -35, left: 0, bottom: 0, right: 0)
I don't think it's a good solution case the real reason is produced by .grouped
. Controlling the whole contentView offset, contentInset
should not be used to offset the effect of header(show below).
When you specify the UICollectionLayoutListConfiguration with .grouped
mode,
Without any other code your listConfig
means listConfig.headerMode = .none
and listConfig.footerMode = .none
by default. The collectionView will produce a header and a footer for each section .
The 35 pixel comes from the height of your section header.In this case, I guess you only have one section, and as you are able to see, you must have the same extra padding at the bottom.
1、listConfig.headerMode = .firstItemInSection
The convenient and simplest way
When you use this header mode, a UICollectionViewListCell object that appears as the first item in the section automatically uses a header appearance. When you configure your data source, make sure to account for the fact that the first item in the section (at index 0) represents the header, and the actual items in the section start at index 1.
2、listConfig.headerMode = .supplementary
You may totally custom your header within UICollectionViewDataSource
func collectionView(_ collectionView: UICollectionView, viewForSupplementaryElementOfKind kind: String, at indexPath: IndexPath) -> UICollectionReusableView {
if (kind == UICollectionView.elementKindSectionHeader) {
let header = collectionView.dequeueReusableSupplementaryView(ofKind: UICollectionView.elementKindSectionHeader, withReuseIdentifier: "your headerIdentifier", for: indexPath)
... // do some custom things
return header
}
return UICollectionReusableView()
}
and don't forget this
collectionView.register(UICollectionReusableView.self, forSupplementaryViewOfKind: UICollectionView.elementKindSectionHeader, withReuseIdentifier: "your headerIdentifier")