Adjust UICollectionViewCell to fit content

2019-07-01 16:53发布

I have a label that is set to resize to its content. I want the UICollectionViewCell to do the same. I cannot find anyway to implement that.

A picture describing my current arrangement can be found below:

enter image description here

3条回答
萌系小妹纸
2楼-- · 2019-07-01 17:41

Use "Autolayout" in cell's Interface Builder document. After setting constraints, your cell frame is going to dynamically change. enter image description here

查看更多
ら.Afraid
3楼-- · 2019-07-01 17:43

configure collection:

YOU_Collection.layout.estimatedItemSize = CGSize(width: 1, height: 1)

and in cell add label constraints (top and bottom)

查看更多
smile是对你的礼貌
4楼-- · 2019-07-01 17:55

I think you'll want to implement the UICollectionViewDelegateFlowLayout method collectionView:layout:sizeForItemAtIndexPath: to adjust the size of each collection item based on the contents.

Here's a tutorial on Collection Views that covers this specific topic.

查看更多
登录 后发表回答