如何纯代码给UICollectionView添加HeaderView

2025-05-19 16:44:02
推荐回答(1个)
回答(1):

从 UICollectionReusableView 继承。
如 @interface HeaderCRView : UICollectionReusableView

然后在Vc里面通过registerClass把该类注册进去
如 [self.collectionView registerClass:[HeaderCRView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:kheaderIdentifier];

当然这个设置不能忘记
-(CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section{
CGSize size={320,45};
return size;
}