Raf*_*fAl 4 objective-c ios uicollectionview
我正在尝试在一个中实现一个粘性标头UICollectionView.
我的粘性标题行为与您可以看到的通常情况略有不同,例如UITableView.我在集合视图中有3个标题,我希望其中只有一个是粘性的,并在滚动内容时粘在顶部.
我的代码工作得很好.但是,当我向下滚动时,粘性标题会在某个时刻突然消失.向后滚动会使标题再次出现.我究竟做错了什么?
我附加了自定义布局的实现.它是的子类UICollectionViewFlowLayout.
@implementation CustomFlowLayout
- (NSArray *)layoutAttributesForElementsInRect:(CGRect)rect
{
NSMutableArray *attributes = [[super layoutAttributesForElementsInRect:rect] mutableCopy];
CGPoint const contentOffset = self.collectionView.contentOffset;
for (UICollectionViewLayoutAttributes *layoutAttributes in attributes)
{
// Adjust the sticky header frame.
if ([layoutAttributes.representedElementKind isEqualToString:UICollectionElementKindSectionHeader] &&
layoutAttributes.indexPath.section == SectionWithStickyHeadeIndex)
{
NSInteger numberOfItemsInSection = [self.collectionView numberOfItemsInSection:SectionWithStickyHeadeIndex];
NSIndexPath *firstObjectIndexPath = [NSIndexPath indexPathForItem:0
inSection:SectionWithStickyHeadeIndex];
UICollectionViewLayoutAttributes *firstObjectAttrs;
if (numberOfItemsInSection > 0)
{
firstObjectAttrs = [self layoutAttributesForItemAtIndexPath:firstObjectIndexPath];
}
else
{
firstObjectAttrs = [self layoutAttributesForSupplementaryViewOfKind:UICollectionElementKindSectionHeader
atIndexPath:firstObjectIndexPath];
}
CGPoint origin = layoutAttributes.frame.origin;
// Adjust the header origin so it sticks to the top.
origin.y = MAX(contentOffset.y + self.collectionView.contentInset.top,
CGRectGetMinY(firstObjectAttrs.frame) - CGRectGetHeight(layoutAttributes.frame));
layoutAttributes.zIndex = CGFLOAT_MAX;
layoutAttributes.frame = (CGRect)
{
.origin = origin,
.size = layoutAttributes.frame.size
};
break;
}
}
return attributes;
}
- (BOOL)shouldInvalidateLayoutForBoundsChange:(CGRect)newBound
{
return YES;
}
@end
Run Code Online (Sandbox Code Playgroud)
我对此并不是百分之百确定,但看起来就像你向下滚动得足够远,标题的原始位置不再位于rect参数内.这导致标题的布局属性不包含在循环中attributes迭代的数组中for,导致布局位置不再调整到屏幕顶部的"粘性"位置.
尝试在for循环之前添加这些行,以便将粘滞标题的布局属性添加到attributes数组中(如果它们尚不存在):
Run Code Online (Sandbox Code Playgroud)NSIndexPath *stickyHeaderIndexPath = [NSIndexPath indexPathForItem:0 inSection:SectionWithStickyHeaderIndex]; UICollectionViewLayoutAttributes *layoutAttributes = [self layoutAttributesForSupplementaryViewOfKind:UICollectionElementKindSectionHeader atIndexPath:stickyHeaderIndexPath]; if (![attributes containsObject:layoutAttributes]) { [attributes addObject:layoutAttributes]; }
| 归档时间: |
|
| 查看次数: |
1189 次 |
| 最近记录: |