动态改变UICollectionReuseableView(集合节标题)的高度

31

我正在尝试动态地设置UICollectionView的节头高度,但是使用下面的代码,我并没有看到任何改变。视图以正确的项目绘制,但高度不会改变。如果这是一个重复的问题,我很抱歉,但我似乎找不到与UICollectionView对象具体相关的任何内容。提前致谢。

- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView
           viewForSupplementaryElementOfKind:(NSString *)kind
                                 atIndexPath:(NSIndexPath *)indexPath
{
    PhotoVideoHeaderCell *headerView = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader
                                                                          withReuseIdentifier:@"videoHeaderView"
                                                                                 forIndexPath:indexPath];
    if (indexPath.section == 0) {
        // photos
        [headerView setSection:@"Photo"];
    } else {
        [headerView.VehicleDetailView removeFromSuperview];
        CGRect frame = headerView.frame;
        frame.size.height = 60;
        [headerView setFrame:frame];
        [headerView setNeedsDisplay];
        [headerView setBackgroundColor:[UIColor grayColor]];
        [headerView setSection:@"Video"];
    }

    return headerView;
}
4个回答

77

假设你正在使用流式布局,你的代理应该实现以下函数:

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section;
您可以为每个标题返回不同的大小。在水平滚动的集合视图中,只使用宽度。在垂直滚动的集合视图中,仅使用高度。未使用的值将被忽略 - 您的视图将始终被拉伸以填充水平/垂直集合视图的整个高度/宽度。
脚注也有相应的方法。

1
这个可以用,但是我怎样知道我正在编辑哪个部分?我只想针对索引为1的部分进行编辑。 - DirtyKalb
在方法中使用“section”参数。将您正在编辑的部分存储在ivar中,然后进行比较。如果它们不同,则返回布局的“headerReferenceSize”。 - Ash Furrow
抱歉我在这里是个完全的新手,但headerReferenceSize不是collectionViewLayout的一部分...我需要创建一个header实例来读取frame吗? - DirtyKalb
3
不行 - 将collectionViewLayout强制转换为UICollectionViewFlowLayout并返回headerReferenceSize,代码如下:return [(UICollectionViewFlowLayout *)self.collectionView.collectionViewLayout headerReferenceSize]; - Ash Furrow
英雄再次出现@AshFurrow - 这正是我想要的,太完美了 :)谢谢! - Henry Heleine

6

在Swift 3和4中被接受的答案:

func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, referenceSizeForHeaderInSection section: Int) -> CGSize {
    return CGSize(width: collectionView.frame.size.width, height: 250)
}

5

相比使用嘈杂的委托方法,我更喜欢使用以下方法:(仅在具有唯一标头大小时有效)

    let layout = self.collectionView.collectionViewLayout as! UICollectionViewFlowLayout // Assuming you use the default flow layout 
    layout.headerReferenceSize = CGSize(width: 42, height: 42) //Set the header size

该功能也适用于itemSize:

    layout.itemSize = CGSize(width: 42, height: 42) //Set a custom item size

这对于设置一个相对于屏幕宽度的项目大小非常有效。


1

尝试类似这样的东西:

- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath {
    UICollectionReusableView *header = [siteMapCollection dequeueReusableSupplementaryViewOfKind: UICollectionElementKindSectionHeader withReuseIdentifier: @"headerID" forIndexPath: indexPath];
    NSString *headerText = @"This is my header";
    UIFont *labFont = [UIFont fontWithName: @"HelveticaNeue-CondensedBold" size: 20.0];
    CGSize textSize = [dummyText sizeWithFont: labFont];
    UILabel *headerLabel = [[UILabel alloc] initWithFrame: CGRectMake(0, header.frame.size.height - (textSize.height + 12), header.frame.size.width, textSize.height + 8)];
    [headerLabel setFont: labFont];

    [header addSubview: headerLabel];

    return header;
}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接