UICollectionView and Supplementary View (header)

前端 未结 10 1120
南旧
南旧 2021-02-04 05:17

Trying to add a a Supplementary view into my UICollectionView as a header. I\'m having issues getting it to work.

I use a custom UICollectionViewFlowL

10条回答
  •  难免孤独
    2021-02-04 05:35

    I was having the same issue and similarly, I was not registering the header correctly. there are several threads on here that all suggest using a Nib to define the header but I have done it differently and is working fine.

    I have a custom header SizeCollectionHeader, which inherits the UICollectionReusableView. It is registered like this.

        [self.collectionView registerClass:[GRSizeCollectionHeader class] forSupplementaryViewOfKind:@"UICollectionElementKindSectionHeader"
                                                                                 withReuseIdentifier:@"SupplementaryViewIdentifier"];
    

    and is working fine.

    my initial problem was that had a random "Kind" value like this.

        [self.collectionView registerClass:[GRSizeCollectionHeader class] forSupplementaryViewOfKind:@"SupplementaryViewKind"
                                                                                 withReuseIdentifier:@"SupplementaryViewIdentifier"];
    

    This will crash.

    So I just wanted to comment for anybody that is looking here that you don't need to use a nib.

提交回复
热议问题