【发布时间】:2018-11-28 18:03:10
【问题描述】:
我有一个 UICollectionView,它会在单击单元格时展开,一旦屏幕填满,它就会变成可滚动的。
现在,当我向下滚动时,我需要我的标题视图随之向下滚动,为此我在我的自定义 UICollectionViewLayout 类中实现了 layoutAttributesForSupplementaryViewOfKind 方法中的逻辑。
这很好,但现在的问题是,当我的内容变得可滚动并且我向下滚动几个单元格并立即单击一个单元格以将内容缩小回一个屏幕时,标题视图没有得到安排,即它仍然保持在最后滚动的位置。
但是之后,如果我执行任何其他操作,例如单元格点击,它就会得到正确安排。
我尝试调用setNeedsLayout、setNeedsDisplay 和layoutSubviews 重新加载我的 UICollectionView 但标题仍然没有更新到正确的位置。
下面是我的layoutAttributesForSupplementaryViewOfKind 方法的代码。
感谢任何帮助。
- (UICollectionViewLayoutAttributes *)layoutAttributesForSupplementaryViewOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath {
if (![kind isEqualToString:[myGridHeaderView kind]]) {
return nil;
}
myGridHeaderPosition headerPosition = [[self collectionView] headerPositionAtIndexPath:indexPath];
CGRect cellRect = [[self delegate] getRectForHeaderAtIndex:indexPath headerPosition:headerPosition];
if (CGRectEqualToRect(cellRect, CGRectZero)) {
return nil;
}
myGridHeaderLayoutAttribute* attributes = [myGridHeaderLayoutAttribute layoutAttributesForSupplementaryViewOfKind:kind withIndexPath:indexPath];
CGPoint centerPoint = CGPointMake(CGRectGetMidX(cellRect), CGRectGetMidY(cellRect));
CGSize size = cellRect.size;
UICollectionView * const cv = self.collectionView;
NSInteger zIndex = 1;
CGPoint const contentOffset = cv.contentOffset;
if (contentOffset.x > 0)
{
if (headerPosition != myGridHeaderPositionColumn)
{
centerPoint.x += contentOffset.x;
}
zIndex = 1005;
}
if (contentOffset.y > 0)
{
if (headerPosition != myGridHeaderPositionRow)
{
centerPoint.y += contentOffset.y;
}
zIndex = 1005;
}
if (headerPosition == myGridHeaderPositionCommon) {
zIndex = 1024;
}
attributes.zIndex = zIndex;
attributes.headerPosition = headerPosition;
attributes.center = centerPoint;
attributes.size = size;
attributes.alpha = 1.0;
return attributes;
}
【问题讨论】:
标签: ios objective-c uicollectionview uicollectionviewlayout xcode9.3