【问题标题】:change the height of a UICollectionReuseableView (collection section header) dynamically动态更改 UICollectionReuseableView(集合部分标题)的高度
【发布时间】:2013-01-17 16:48:50
【问题描述】:

我正在尝试为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;
}

【问题讨论】:

    标签: objective-c ios6 xcode4.5


    【解决方案1】:

    假设您使用的是流式布局,您的委托应实现以下功能:

    - (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section;
    

    您可以为每个标题返回不同的大小。在水平滚动的集合视图中,仅使用宽度。在垂直滚动中,仅使用高度。未使用的值将被忽略 - 您的视图将始终被拉伸以分别填充水平/垂直集合视图的整个高度/宽度。

    页脚也有相应的方法。

    【讨论】:

    • 这行得通,但是我怎么知道我正在编辑哪个部分,我只想为索引 1 处的部分做这件事
    • 在方法中使用section参数。将您正在编辑的部分存储在 ivar 中,然后进行比较。如果它们不相同,则返回布局的headerReferenceSize
    • 很抱歉在这里是一个完整的菜鸟,但 headerReferenceSize 不是 collectionViewLayout 的一部分...我需要创建一个标题实例来读取帧吗?
    • 不 - 投射 collectionViewLayoutreturn [(UICollectionViewFlowLayout *)self.collectionView.collectionViewLayout headerReferenceSize];
    • 再次成为英雄@AshFurrow - 这正是我所追求的,完美:) 谢谢!
    【解决方案2】:

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

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

    【讨论】:

      【解决方案3】:

      好吧,我更喜欢使用这种方法,而不是使用嘈杂的委托方法:(仅当您具有唯一的标头大小时才有效)

          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
      

      例如,这对于设置相对于屏幕宽度的项目大小非常有用。

      【讨论】:

        【解决方案4】:

        试试这样的:

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

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2015-09-15
          • 2012-09-25
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2017-01-30
          • 2013-07-16
          相关资源
          最近更新 更多