【发布时间】:2014-08-07 12:30:04
【问题描述】:
我实现了一个在顶部有一个粘性装饰视图的流布局。这是通过继承 UICollectionViewFlowLayout 并覆盖以下方法来实现的:
- (BOOL)shouldInvalidateLayoutForBoundsChange:(CGRect)newBounds {
return YES;
}
和
- (NSArray *)layoutAttributesForElementsInRect:(CGRect)rect {
NSMutableArray *result = [[super layoutAttributesForElementsInRect:rect] mutableCopy];
[result addObject:[self stickyHeaderLayoutAttributes]];
return result;
}
(在内部,装饰视图的布局属性存储为属性,并且 -stickyHeaderLayoutAttributes 更新其框架以始终显示在集合视图的顶部)。
现在我在集合视图控制器的导航栏上有一个切换按钮,它使用自定义流布局显示一些数据。一键切换按钮应通过在导航栏下滑动来隐藏装饰视图。第二次点击应该会显示它(再次从导航栏下方滑动)。
我隐藏装饰视图的方法如下(它以类似的方式显示它):
- 点击按钮时,告诉布局隐藏装饰视图,例如通过设置标志。
-
调用
[self.collectionView performBatchUpdates:nil completion:nil]; - 在 layoutAttributesForElementsInRect: 中不要为装饰视图传递布局属性。
- finalLayoutAttributesForDisappearingSupplementaryElementOfKind:atIndexPath: 被调用,我将框架设置在导航栏下方。
- collection view 移除装饰 view 并滑动到导航栏下方。
不过,我注意到的是,仅在第 2 步中,finalLayoutAttributesForDisappearingSupplementaryElementOfKind:atIndexPath: 被调用,这意味着装饰视图应该被删除。
为什么会这样?
indexPathsToDeleteForSupplementaryViewOfKind: 返回一个空数组,因此我不清楚为什么要删除装饰视图。
有没有更好的方法来解决这个问题?
【问题讨论】:
标签: ios uicollectionview uicollectionviewlayout