I'm creating a grouped UICollectionView, and it seems that the top has 35 pixels of extra padding at the top I don't know how to get rid of.
It appears this is also a problem with UITableView and I've found some questions to address it here and here, but those solutions don't work for UICollectionView.
Here's how I initialize my UICollectionView:
var listConfig = UICollectionLayoutListConfiguration(appearance: .grouped)
UICollectionView(frame: .zero, collectionViewLayout: UICollectionViewCompositionalLayout.list(using: listConfig))
// More autolayout constraints code later
So far to fix this I've been applying a -35 constant to the topAnchor, but that doesn't work in all cases (when I want something directly above in the same view).
I also tried listConfig.headerTopPadding = 0 with no success.
How can I get rid of this extra top padding? (And side question...why does that exist?)
As you mentioned here
I don't think it's a good solution case the real reason is produced by
.grouped. Controlling the whole contentView offset,contentInsetshould not be used to offset the effect of header(show below).When you specify the UICollectionLayoutListConfiguration with
.groupedmode, Without any other code yourlistConfigmeanslistConfig.headerMode = .noneandlistConfig.footerMode = .noneby 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 = .firstItemInSectionThe convenient and simplest way
2、
listConfig.headerMode = .supplementaryYou may totally custom your header within UICollectionViewDataSource
and don't forget this