[英]How do I dynamically size UICollectionViewCell's height based on content?
What's the process for doing this? 这样做的过程是什么?
I've discovered sizeForItemAtIndexPath
but I'm not sure what I do in that method. 我发现了sizeForItemAtIndexPath
但我不确定我在该方法中做了什么。 Do I create a new UICollectionViewCell
, add in my content, and then return the size of that cell? 我是否创建了一个新的UICollectionViewCell
,添加我的内容,然后返回该单元格的大小?
Do I need AutoLayout turned on and constraints set? 我是否需要打开AutoLayout并设置约束? I apologize if this has been answered already. 如果已经回答,我道歉。 I've been scouring stackoverflow, but haven't quite found a solution that worked. 我一直在搜索stackoverflow,但还没有找到一个有效的解决方案。
You need to: 你需要:
setup Auto Layout constraints on the cell's contentView
that, taken all in all, bind the left edge to the right edge, and the top edge to the bottom edge. 在单元格的contentView
上设置自动布局约束, contentView
上将左边缘绑定到右边缘,将上边缘绑定到底边缘。 This is how the cell will express the size it wants to be. 这就是细胞如何表达它想要的大小。
not implement sizeForItemAtIndexPath
, since you will not be providing sizes on a per-cell basis. 不实现sizeForItemAtIndexPath
,因为您不会在每个单元格的基础上提供大小。
set estimatedItemSize
to some reasonably large value on your UICollectionViewFlowLayout
object. 设置estimatedItemSize
到你在某些方面相当大的价值UICollectionViewFlowLayout
对象。
There is a repo with an example . 有一个例子的回购 。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.