UICollectionView header not showing

Did you implement:

- (UICollectionViewLayoutAttributes *)layoutAttributesForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath

There's a ton of methods to implement just to make one thing work...I'm learning too. Tell me if it works.

Edit: Sorry wrong method. That is for subclassing I think. The one I'm talking about is in UICollectionViewLayout (the layout object you subclass, if your layout supports supplementary views):

- layoutAttributesForSupplementaryViewOfKind:atIndexPath:

See here: https://developer.apple.com/library/ios/#documentation/UIKit/Reference/UICollectionViewLayout_class/Reference/Reference.html#//apple_ref/occ/cl/UICollectionViewLayout


for SWIFT 3 & SWIFT 4

    self.collectionView.register(UINib(nibName: "SectionCollectionReusableView", bundle: nil), forSupplementaryViewOfKind: UICollectionElementKindSectionHeader, withReuseIdentifier: "SectionCollectionReusableView")



    self.collectionView.fs_width = self.collectionView.bounds.width

    let layout: UICollectionViewFlowLayout = UICollectionViewFlowLayout()
    layout.sectionInset = UIEdgeInsets(top: 70, left: 40, bottom: 0, right: 0)
    layout.minimumInteritemSpacing = 0
    layout.minimumLineSpacing = 0
    layout.estimatedItemSize = CGSize(width: 350, height: 140)
    layout.scrollDirection = .horizontal
    layout.headerReferenceSize = CGSize(width: self.collectionView.bounds.size.width, height: 60)
    layout.sectionHeadersPinToVisibleBounds = true
    self.collectionView!.collectionViewLayout = layout

You have to add this to ViewDidLoad, and notice the

layout.headerReferenceSize = CGSize(width: self.collectionView.bounds.size.width, height: 60)

This will make the header Section Layouts

and Thanks @Guido Hendriks, and all I have got the insight from their answer as well


After looking for the method yuf asked about, I read that by default the size of headers/footers are 0,0. If the size is 0, the header/footer won't display.

You can set the size with a property:

flowLayout.headerReferenceSize = CGSizeMake(0, 100);

Then all the headers will have the same size. If it has to be different for each section, you can implement the following method, which is part of the UICollectionViewDelegateFlowLayout protocol.

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section {
    if (section == albumSection) {
        return CGSizeMake(0, 100);
    }

    return CGSizeZero;
}

Note that in vertical scrolling it uses the returned height and the full width of the collection view, in horizontal scrolling it uses the return width and the full height of the collection view.