UICollectionViewFlowLayout vs子类化UICollectionViewLayout [英] UICollectionViewFlowLayout vs subclassing UICollectionViewLayout
问题描述
我知道如果您正在执行任何像网格或基于行的分解布局,则记录的建议是使用UICollectionView Flow 布局。但是,我不确定这是否适用于我的情况。
I know the documented advice is to use UICollectionViewFlowLayout if you are doing anything "like a grid or a line-based breaking layout". However, I am not sure this is true for my case.
我想要一个网格,但不想要一个换行符布局。物品应无限制地水平和垂直布置,无需堆叠。基本上,如果内容超出框架,则可以水平或垂直滚动的巨型棋盘。
I want a grid but do not want a line-breaking layout. Items should be laid out infinitely both horizontally and vertically without ever stacking. Essentially, a giant chessboard that scrolls horizontally or vertically if content goes beyond the frame.
要继承UICollectionView Flow 布局,我必须:
To subclass UICollectionViewFlowLayout I would have to:
- 覆盖
prepareLayout
以阻止布局包装项目。这似乎很多工作。 - 覆盖
collectionViewContentSize
。
- Override
prepareLayout
to stop the layout from wrapping items. This seems like a lot of work. - Override
collectionViewContentSize
.
Apple说他们在制作UICollectionView Flow 布局方面做了很多艰苦的工作,所以如果可以,我应该利用它。但是如果我必须覆盖 prepareLayout
来关闭换行符,我怀疑我丢掉了他们大部分的工作。在剩下的工作中,我可能不会使用大部分工作(例如, minimumLineSpacingForSectionAtIndex
)。
Apple says they have done "lots of hard work" in crafting UICollectionViewFlowLayout, so I should leverage it if I can. But if I have to override prepareLayout
to turn off line-breaking, I suspect that I am throwing away a large part of their work. Of their work that is left, I probably will not use most of it anyway (for example, minimumLineSpacingForSectionAtIndex
).
因为我想要的布局非常简单,我怀疑我应该继承UICollectionViewLayout,因为:
Because the layout I want is so simple, I suspect that I should subclass UICollectionViewLayout instead, because:
- 我会更简单,更清洁在一个布局类中实现所有内容而不是在子类和委托之间传播。
- 我认为它不会比继承UICollectionView Flow 更难布局,因为我必须在两种情况下都覆盖
prepareLayout
,我怀疑这是所有艰苦工作的地方。 - 我'能够以自定义方式调整其他UICollectionViewLayoutAttributes比尝试在UICollectionView Flow 布局子类之上添加另一个kludge更好。
- I will have a simpler and cleaner implementation with everything in one layout class instead of spread between a subclass and a delegate.
- I don't think it will be that much harder than subclassing UICollectionViewFlowLayout because I have to override
prepareLayout
in both cases, and I suspect that is where all the hard work will be. - I'll be in a better position to tweak other UICollectionViewLayoutAttributes in custom ways than trying to add another kludge on top of a UICollectionViewFlowLayout subclass.
我的结论是否正确?
推荐答案
UICollectionVie wFlowLayout 无论如何都不能支持两个方向,它只能沿着一个轴滚动,水平或垂直。所以你必须继承 UICollectionViewLayout 而不是 UICollectionViewFlowLayout 。
UICollectionViewFlowLayout can't support two directions anyway, it scrolls along one axis only, either horizontally or vertically. So you have to subclass UICollectionViewLayout not UICollectionViewFlowLayout.
然后你必须覆盖 prepareForLayout , layoutsAttributesForElementsInRect 方法正如您所说..
Then you have to override prepareForLayout, layoutsAttributesForElementsInRect methods as you said correctly..
这篇关于UICollectionViewFlowLayout vs子类化UICollectionViewLayout的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!