5

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

spogebob92
  • 1,474
  • 4
  • 23
  • 32

3 Answers3

2

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.

Chris Goldman
  • 307
  • 1
  • 10
  • 1
    `collectionView:layout:sizeForItemAtIndexPath: ` just allows you set size dynamically in points, it has nothing to do with height based on content – user924 Apr 14 '19 at 13:22
  • Also I have 2 sections in UIControllerView (2 columns). Two cells in one row should have the same size even if their size content is different (something like this is implemented in Android natively when using RecyclerView with GridLayoutManager, very easy) – user924 Apr 14 '19 at 15:50
1

configure collection:

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

and in cell add label constraints (top and bottom)

ober
  • 2,363
  • 1
  • 19
  • 17
0

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

Gokhan Gultekin
  • 291
  • 5
  • 15
  • it's not true: https://stackoverflow.com/questions/55676984/make-uicollectionviewcells-height-match-its-content-sum-height-of-all-sub-view – user924 Apr 14 '19 at 15:49