Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

UICollectionView: How to add space between Footer and Header?

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

Now there's zero pixels between the footer and the header views (i.e between the sections). I'd like to add a little spacing between them, but not above the first section or after the last one. So I can't just add that space in the header and footer views itself.

I would have expected something like "interSectionSpacing", but apparently there is no such setting. Any ideas?

like image 904
Dorian Roy Avatar asked Oct 02 '12 09:10

Dorian Roy


People also ask

How do you add header and footer view in UICollectionView?

Select the Collection Reusable View of the header, set the identifier as “HeaderView” in the Attributes inspector. For the Collection Reusable View of the footer, set the identifier as “FooterView”.

How to add header to UICollectionView?

There are no section headers in the UICollectionView. So for your first task, you'll add a new section header using the search text as the section title. To display this section header, you'll use UICollectionReusableView .

What is Section inset?

Section insets are margins applied only to the items in the section. They represent the distance between the header view and the first line of items and between the last line of items and the footer view. They also indicate the spacing on either side of a single line of items.

What is Uicollectionviewflowlayout?

A layout object that organizes items into a grid with optional header and footer views for each section.


2 Answers

So it turns out there is no setting for that. This is what I ended up with:

I set the content of my header view to be aligned to the bottom of the header view itself, so it seems to have the same visible height, even if I make the header view taller than its content (auto layout makes this really easy).

Then I set the height of the header depending on the section index in this delegate 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's a little space between the sections, but not before the first section.

like image 169
Dorian Roy Avatar answered Sep 19 '22 08:09

Dorian Roy


You're right there's no such thing as "interSectionSpacing", but there is something close.

Try sectionInset on your UICollectionViewFlowLayout.

UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc] init];
[flowLayout setSectionInset:UIEdgeInsetsMake(top, left, bottom, right)];
like image 34
Alexander Avatar answered Sep 19 '22 08:09

Alexander