向 UICollectionview 添加一个顶部标题,该标题已经包含带有标题的部分
Posted
技术标签:
【中文标题】向 UICollectionview 添加一个顶部标题,该标题已经包含带有标题的部分【英文标题】:Adding a top header to UICollectionview that already has sections with headers 【发布时间】: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 的集合视图上方创建一个视图并使用自动布局以编程方式更改它,您可以选择吗? 是的,如果您愿意,请发布详细的答案。 【参考方案1】:我会执行这些步骤来完成它:
在您的故事板中,创建一个 UIView 并根据需要设计它的子视图。请确保您选中了刚刚创建的 UIView 的 clipToBounds
复选框。
创建您的约束。我猜它应该是 top 0,left 0,right 0,当然还有 height 0(因为这是你提到的初始状态)。
为高度约束创建 IBOutlet 并将其连接到您刚刚创建的高度约束。我们就叫它viewHeaderHeightConstraint
吧。
当你想显示这个视图时:
self.viewHeaderHeightConstraint.constant = 50; // Or any other value
[self.view layoutIfNeeded];
如果你想动画高度变化:
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:
中为第一部分返回更大的尺寸。
【讨论】:
以上是关于向 UICollectionview 添加一个顶部标题,该标题已经包含带有标题的部分的主要内容,如果未能解决你的问题,请参考以下文章
当在顶部添加新帖子时,UICollectionView 单元格自定义 FlowLayout 中断
如何在 UICollectionView 顶部添加一个视图以支持诸如搜索栏或 Objective-c 中的过滤器之类的内容?
当有人滚动到 UICollectionView 的顶部时,如何调用方法?
无法使用 xCode 向 UiCollectionView 添加标题部分
如何在UICollectionView中添加HeaderView,如UITableView的tableHeaderView