UICollectionView header not showing

前端 未结 3 1404
感动是毒
感动是毒 2021-01-31 14:21

I\'m working on a project that uses an UICollectionView to show several albums. The items show fine, but now I want to show an header above the first section.

3条回答
  •  遥遥无期
    2021-01-31 15:08

    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.

提交回复
热议问题