UICollectionViewFlowLayout 与子类化 UICollectionViewLayout [英] UICollectionViewFlowLayout vs subclassing UICollectionViewLayout

查看:10
本文介绍了UICollectionViewFlowLayout 与子类化 UICollectionViewLayout的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道记录在案的建议是使用 UICollectionViewFlowLayout 如果您正在做任何像网格或基于行的中断布局".但是,我不确定这是否适用于我的情况.

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.

要继承 UICollectionViewFlowLayout 我必须:

To subclass UICollectionViewFlowLayout I would have to:

  1. 覆盖 prepareLayout 以阻止布局包装项目.这似乎需要做很多工作.
  2. 覆盖 collectionViewContentSize.
  1. Override prepareLayout to stop the layout from wrapping items. This seems like a lot of work.
  2. Override collectionViewContentSize.

Apple 表示他们在制作 UICollectionViewFlowLayout 方面做了很多艰苦的工作",所以如果可以的话,我应该利用它.但是如果我必须重写 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:

  1. 我将有一个更简单、更清晰的实现,所有内容都在一个布局类中,而不是分散在子类和委托之间.
  2. 我认为这不会比继承 UICollectionViewFlowLayout 困难得多,因为在这两种情况下我都必须重写 prepareLayout,我怀疑这就是所有辛苦了.
  3. 与尝试在 UICollectionViewFlowLayout 子类之上添加另一个组件相比,我将能够以自定义方式调整其他 UICollectionViewLayoutAttributes.
  1. I will have a simpler and cleaner implementation with everything in one layout class instead of spread between a subclass and a delegate.
  2. 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.
  3. 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.

我的结论正确吗?

推荐答案

UICollectionViewFlowLayout 无论如何也不支持两个方向,它只能沿一个轴滚动,水平或垂直.所以你必须继承 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.

然后你必须重写 prepareForLayoutlayoutsAttributesForElementsInRect 方法,正如你所说的那样......

Then you have to override prepareForLayout, layoutsAttributesForElementsInRect methods as you said correctly..

这篇关于UICollectionViewFlowLayout 与子类化 UICollectionViewLayout的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆