configurationUICollectionViewFlowLayout从底部到顶部布置行
默认情况下(即垂直滚动方向),UICollectionViewFlowLayout从左上angular开始,从左到右,直到行被填充,然后进入下一行。 相反,我希望它从左到右,从左到右,直到行被填充,然后继续下一个行。
有没有一个简单的方法来做到这一点,通过inheritanceUIScrollViewFlowLayout,或者我基本上需要从头重新实现该类?
苹果关于子类stream布局的文档表明,我只需要重写和重新实现我自己版本的layoutAttributesForElementsInRect:
, layoutAttributesForItemAtIndexPath:
和collectionViewContentSize
。 但这似乎并不简单。 由于UICollectionViewFlowLayout不公开任何网格布局计算,它在prepareLayout
进行内部prepareLayout
,所以我需要从它为顶部到底部布局生成的值推导出底部到顶部布局所需的所有布局值。
我不确定这是可能的。 虽然我可以重新使用它的计算关于哪些组项目被放在同一行,我将需要计算新的y偏移量。 为了使我的计算,我将需要有关所有项目的信息,但这些超类方法不报告。
你基本上可以用一个简单的逻辑来实现它,但是这似乎有点奇怪。 如果collectionview内容与collectionview边界相同,或者所有单元格都可见,则可以使用简单的flowLayout来实现这一点,
@implementation SimpleFlowLayout - (UICollectionViewLayoutAttributes*)layoutAttributesForItemAtIndexPath:(NSIndexPath *)indexPath{ UICollectionViewLayoutAttributes *attribute = [super layoutAttributesForItemAtIndexPath:indexPath]; [self modifyLayoutAttribute:attribute]; return attribute; } - (NSArray*)layoutAttributesForElementsInRect:(CGRect)rect{ NSArray *attributes = [super layoutAttributesForElementsInRect:rect]; for(UICollectionViewLayoutAttributes *attribute in attributes){ [self modifyLayoutAttribute:attribute]; } return attributes; } - (void)modifyLayoutAttribute:(UICollectionViewLayoutAttributes*)attribute{ CGSize contentSize = self.collectionViewContentSize; CGRect frame = attribute.frame; frame.origin.x = contentSize.width - attribute.frame.origin.x - attribute.frame.size.width; frame.origin.y = contentSize.height - attribute.frame.origin.y - attribute.frame.size.height; attribute.frame = frame; } @end
所以这个数字看起来像这样,
但是,如果使用的行数多于同时在屏幕上可以看到的数量,那么似乎还有一些重用的问题。 由于UICollectionView数据源方法collectionView:cellForItemAtIndexPath:线性工作,并在用户滚动时要求索引path,所以在通常增加的索引path模式(例如1 — 100)中询问单元格,但是我们希望它反转该模式。 在滚动的时候,我们需要collectionView以降序的方式询问我们的项目,因为我们的100个项目位于顶部,1个位于底部。 所以,我没有什么特别的想法可以做到这一点。
通过@ insane-36非常有用的答案显示了一个方法来做到这一点时, collectionView.bounds == collectionView.collectionViewContentSize
。
但是,如果您希望支持collectionView.bounds < collectionViewcontentSize
的情况,那么我相信您需要重新映射rects以正确支持滚动。 如果您希望支持collectionView.bounds > collectionViewContentSize
的情况,那么您需要重写collectionViewContentSize
以确保内容rect位于collectionView的底部(否则它将位于顶部,因为top-to UIScrollView
底部默认行为)。
所以完整的解决scheme有一点涉及,我最终在这里开发它: https : //github.com/algal/ALGReversedFlowLayout 。