【问题标题】:How to add Header with Supplementary view to UiCollectionView in Objective-C?如何在 Objective-C 中将带有补充视图的标题添加到 UiCollectionView?
【发布时间】:2017-09-16 13:44:58
【问题描述】:

我有一个包含 CollectionView 的 ViewController :

- (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];
}

但运行后屏幕上没有任何标题。

【问题讨论】:

    标签: ios objective-c uicollectionview header


    【解决方案1】:

    您是否对标头进行了所需的实现?

    https://developer.apple.com/documentation/uikit/uicollectionviewdelegateflowlayout/1617702-collectionview?language=objc

    collectionView:layout:referenceSizeForHeaderInSection:

    返回值 标头的大小。如果返回大小为 (0, 0) 的值,则不添加任何标头。

    讨论 如果不实现此方法,流布局将使用其中的值 标头参考尺寸 属性来设置标题的大小。 在布局期间,仅使用与适当滚动方向相对应的大小。例如,对于垂直滚动方向,布局对象使用您的方法返回的高度值。 (在这种情况下,标题的宽度将设置为集合视图的宽度。)如果相应滚动维度中的大小为 0,则不添加标题。

    【讨论】:

      【解决方案2】:

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2013-02-20
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2017-07-24
        • 1970-01-01
        • 1970-01-01
        • 2021-12-20
        相关资源
        最近更新 更多