【问题标题】:Adding a top header to UICollectionview that already has sections with headers向 UICollectionview 添加一个顶部标题,该标题已经包含带有标题的部分
【发布时间】:2015-04-19 13:45:09
【问题描述】:

假设我有一个包含部分和项目的 UICollectionview。 对于每个部分,我都会生成一个标题,如下所示。

问题:如何将顶部标题视图添加到 collectionView 本身 这与下面演示的部分标题不同(使用objective-c、IB、Auto Layouts)

- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath
{
    UICollectionReusableView *reusableview = nil;




    if (kind == UICollectionElementKindSectionHeader) {
        HeaderCollectionReusableView *headerView = [self.collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"header" forIndexPath:indexPath];


        headerView.label1.text = "This is a section title";

        reusableview = headerView;
    }



    return reusableview;
}

【问题讨论】:

  • 在高度 = 0 的集合视图上方创建一个视图并使用自动布局以编程方式更改它,您可以选择吗?
  • 是的,如果您愿意,请发布详细的答案。

标签: ios objective-c uicollectionview


【解决方案1】:

我会执行这些步骤来完成它:

  1. 在您的故事板中,创建一个 UIView 并根据需要设计它的子视图。请确保您选中了刚刚创建的 UIView 的 clipToBounds 复选框。

  2. 创建您的约束。我猜它应该是 top 0,left 0,right 0,当然还有 height 0(因为这是你提到的初始状态)。

  3. 为高度约束创建 IBOutlet 并将其连接到您刚刚创建的高度约束。我们就叫它viewHeaderHeightConstraint吧。

  4. 当你想显示这个视图时:

    self.viewHeaderHeightConstraint.constant = 50; // Or any other value
    [self.view layoutIfNeeded];
    
  5. 如果你想动画高度变化:

    self.viewHeaderHeightConstraint.constant = 50; // Or any other value
    [UIView animateWithDuration:0.3 animations:^{
    
        [self.view layoutIfNeeded];
    
    } completion:^(BOOL finished) {
    
        // Do something after completion
    
    }];
    

【讨论】:

    【解决方案2】:

    使用您当前的标题视图和一个视图中的顶部标题视图为第一部分生成另一个标题。不要忘记在collectionView:layout:referenceSizeForHeaderInSection: 中为第一部分返回更大的尺寸。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2015-11-07
      • 1970-01-01
      • 2014-01-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-07-12
      相关资源
      最近更新 更多