简体   繁体   English

如何根据内容动态调整UICollectionViewCell的高度?

[英]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: 你需要:

  1. 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. 这就是细胞如何表达它想要的大小。

  2. not implement sizeForItemAtIndexPath , since you will not be providing sizes on a per-cell basis. 实现sizeForItemAtIndexPath ,因为您不会在每个单元格的基础上提供大小。

  3. 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.

相关问题 UICollectionViewCell高度取决于内容大小 - UICollectionViewCell Height depending on content size UICollectionViewCell高度基于标签大小 - UICollectionViewCell height based on label size 如何在内容大小增加时动态增加UICollectionView高度? - How to increase UICollectionView height dynamically as it's content size increases? 如何在Storyboard / Interface Builder中使UIImageView成为UICollectionViewCell高度的一半 - How do I make a UIImageView be half the height of the UICollectionViewCell's height in Storyboard/Interface Builder 如何动态强制UICollectionViewCell的宽度 - How do I force the width of a UICollectionViewCell dynamically 如何根据字符串大小在UITableViewCell中动态设置UITextView的高度? - How to set UITextView's height dynamically in UITableViewCell based on string size? 如何使UICollectionViewCell的高度填充框架的所有大小? - How to make a UICollectionViewCell's height to fill all the size of the frame? 如何根据内容大小获取CollectionViewCell内容高度? - How can I get CollectionViewCell content height based on content size? 如何获得根据 label 内容动态变化的 UIView 的高度 - Swift - How do I get the height of a UIView that dynamically changes based on label content - Swift 我如何在目标C中动态调整UICollectionViewCell的高度 - How can i resize the height of UICollectionViewCell dynamically in objective c
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM