[英]How to automatically adjust height based on label content inside collection view cell in swift?
I want to achieve a self resizing height collection view cell (image above) using swift.我想使用 swift 实现自调整高度集合视图单元格(上图)。 I have tried many options like UICollectionViewDelegateFlowLayout
and sizeforItem
.我尝试了许多选项,例如UICollectionViewDelegateFlowLayout
和sizeforItem
。 Nothing works for me.没有什么对我有用。 I want to achieve similar result like in the image我想获得类似图像中的结果
Set Estimate size to automatic and do not implement sizeForItemAt , In CollectionView Cell, for each label give number of lines to zero.将 Estimate size 设置为自动并且不实现 sizeForItemAt ,在 CollectionView Cell 中,对于每个标签,将行数设为零。 It will resize automatically.它会自动调整大小。 Set Min spacing and line Spacing to zero as well将最小间距和行间距也设置为零
Also, In the collection View cell, three labels should be in stackView with fill allignment and distribution , set multiplier to center label width as 0.6 ( or whatever you want ) of total cell width.此外,在集合视图单元格中,三个标签应该在 stackView 中,填充对齐和分布,将乘数设置为中心标签宽度为总单元格宽度的 0.6(或任何你想要的)。 Give equal width constraint to first and last label like this image :为第一个和最后一个标签提供相等的宽度约束,如下图:
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.