Adjust UICollectionViewCell to fit content

2019-07-01 17:42发布

问题:

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:

回答1:

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.



回答2:

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



回答3:

configure collection:

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

and in cell add label constraints (top and bottom)