UICollectionView中的自定义标题,带有没有Storyboard的Interface Builder [英] Custom Header in UICollectionView with Interface Builder without Storyboard
本文介绍了UICollectionView中的自定义标题,带有没有Storyboard的Interface Builder的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试将自定义视图添加到 UICollectionView
的标题部分。我有xib文件界面构建器,但我不使用storyboard。我已经检查了Interface Builder中的Section Header,但是没有出现任何UICollectionReusableView,我该怎么办?
I'm trying to add a custom view to the header section of my UICollectionView
. I have the xib file interface builder but I don't use storyboard. I have checked the Section Header in Interface Builder but doesn't appear any UICollectionReusableView, what can I do?
推荐答案
最简单的解决方案要以编程方式执行(并将HeaderReusableView保存在另一个XIB文件中):
The easiest solution is to do is programatically (and keep your HeaderReusableView in another XIB file):
[self.collectionView registerNib:[UINib nibWithNibName:@"ItemHeaderView" bundle:nil]
forSupplementaryViewOfKind:UICollectionElementKindSectionHeader
withReuseIdentifier:kItemSectionHeaderViewID];
然后:
- (CGSize) collectionView:(UICollectionView *)collectionView
layout:(UICollectionViewLayout *)collectionViewLayout
referenceSizeForHeaderInSection:(NSInteger)section {
return CGSizeMake(60.0f, 30.0f);// width is ignored
}
当然:
- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView
viewForSupplementaryElementOfKind:(NSString *)kind
atIndexPath:(NSIndexPath *)indexPath {
NSString *productId = nil; ///
ItemHeaderView *view = nil;
view = [collectionView dequeueReusableSupplementaryViewOfKind:kind
withReuseIdentifier:kItemSectionHeaderViewID
forIndexPath:indexPath];
view.titleLabel.text = productId;
view.backgroundColor = [UIColor yellowColor];
return view;
}
这篇关于UICollectionView中的自定义标题,带有没有Storyboard的Interface Builder的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文