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?)
2
Answers
As I was writing this question I found one more stack overflow answer, which finally gave me an answer that worked for me. I used method 4:
This moves my content up and allows me to put things above my collectionView without having problems.
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,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
meanslistConfig.headerMode = .none
andlistConfig.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
2、
listConfig.headerMode = .supplementary
You may totally custom your header within UICollectionViewDataSource
and don’t forget this