UICollectionView: how to add space between footer and header?

I have a CollectionView using the standard FlowLayout class with horizontal scrolling and the header and footer in each section.

Now there are zero pixels between the footer and the header views (between sections). I would like to add a small distance between them, but not above the first section or after the last. Therefore, I can’t just add this space to the header and footer views.

I would expect something like "interSectionSpacing", but apparently there is no such installation. Any ideas?

+7
source share
3 answers

So, it turns out that there are no settings for this. This is what I came across:

I set the contents of my title view to align at the bottom of the title view itself, so it seems to have the same visible height even if I make the title taller than its contents (automatic layout makes it really easy).

Then I set the height of the header depending on the index of the section in this deletive method of UICollectionViewFlowLayout:

#define kHeaderHeight 42 #define kInterSectionMargin 8 - (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section { if (section == 0) { return CGSizeMake(0, kHeaderHeight); } return CGSizeMake(0, kHeaderHeight + kInterSectionMargin); } 

Now there is a small space between sections, but not until the first section.

+9
source

You are right, there is no such thing as "interSectionSpacing", but there is something close.

Try sectionInset on a UICollectionViewFlowLayout .

 UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc] init]; [flowLayout setSectionInset:UIEdgeInsetsMake(top, left, bottom, right)]; 
+1
source

You can set the interval between the header and footer in the Nib file. Go to the "Size inspector" in the "Collection view" and set the desired header height.

0
source

All Articles