Ios 如何将带有补充视图的标题添加到Objective-C中的UiCollectionView?
我有一个包含CollectionView的ViewController:Ios 如何将带有补充视图的标题添加到Objective-C中的UiCollectionView?,ios,objective-c,uicollectionview,header,Ios,Objective C,Uicollectionview,Header,我有一个包含CollectionView的ViewController: - (void)viewDidLoad { UICollectionViewFlowLayout *layout = [UICollectionViewFlowLayout new]; collectionview = [[UICollectionView alloc]initWithFrame:CGRectZero collectionViewLayout:layout]; collectionv
- (void)viewDidLoad
{
UICollectionViewFlowLayout *layout = [UICollectionViewFlowLayout new];
collectionview = [[UICollectionView alloc]initWithFrame:CGRectZero collectionViewLayout:layout];
collectionview.backgroundColor = [UIColor colorWithRed:0.86 green:0.86 blue:0.86 alpha:1.0];
collectionview.translatesAutoresizingMaskIntoConstraints = false;
collectionview.delegate = self;
collectionview.dataSource = self;
collectionview.bounces = true;
[collectionview registerClass:[CollectionViewCell1 class] forCellWithReuseIdentifier:@"cell1"];
[self.view addSubview:collectionview];
[collectionview sdc_alignEdgesWithSuperview:UIRectEdgeAll];
[collectionview registerClass:[RecipeCollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"HeaderView"];
}
-(UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath
{
UICollectionReusableView *reusableview = nil;
RecipeCollectionReusableView *headerView = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"HeaderView" forIndexPath:indexPath];
headerView.backgroundColor = [UIColor greenColor];
headerView.title.text = @"ABC";
reusableview = headerView;
return reusableview;
}
RecipeCollectionReusableView是:
- (void)initialize
{
_title = [UILabel new];
_title.translatesAutoresizingMaskIntoConstraints = false;
[self addSubview:_title];
[_title sdc_centerInSuperview];
}
但运行后屏幕上没有任何标题。您是否为标题执行了所需的实现 集合视图:布局:参考SizeForHeaderInstruction: 返回值 标题的大小。如果返回的值为size(0,0),则不会添加任何标题 讨论 如果未实现此方法,则流布局将使用其 headerReferenceSize 属性设置标头的大小。 在布局期间,仅使用与相应滚动方向相对应的大小。例如,对于垂直滚动方向,布局对象使用方法返回的高度值。(在这种情况下,标题的宽度将设置为集合视图的宽度。)如果相应滚动维度中的大小为0,则不添加标题
这篇文章完全帮助了我: