UICollectionReusableView method not being called

Swift 4 Xcode 9.1 Beta 2

Add @objc

@objc func collectionView(_ collectionView: UICollectionView, layout  collectionViewLayout: UICollectionViewLayout, referenceSizeForFooterInSection section: Int) -> CGSize{
    let size = CGSize(width: 400, height: 50)
    return size
}

Credits: https://medium.com/@zonble/your-delegation-methods-might-not-be-called-in-swift-3-c6065ed7b4cd


You answered the question, but in words I understand better:

To make the method being called, add these methods:

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section {
    return CGSizeMake(60.0f, 30.0f);
}

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout referenceSizeForFooterInSection:(NSInteger)section {
    return CGSizeMake(60.0f, 30.0f);
}

...and go from there.


Did you add UICollectionViewDelegateFlowLayout in current interface? This worked for me.

@interface MyViewController () <UICollectionViewDelegateFlowLayout>

Swift:

class MyViewController: UICollectionViewDelegateFlowLayout {


It seems that you have to give your header a non-zero size or collectionView:viewForSupplementaryElementOfKind:atIndexPath isn't called. So either set the headerReferenceSize property of the flow layout like so:

flowLayout.headerReferenceSize = CGSizeMake(self.collectionView.frame.size.width, 100.f);

Swift 5+

flowLayout.headerReferenceSize = CGSize(CGSize(width: self.collectionView.frame.size.width, height: 100))

or, implement collectionView:layout:referenceSizeForHeaderInSection if you want to vary the size by section.