UICollectionView Self Sizing Cells with Auto Layout

前端 未结 16 1772
野性不改
野性不改 2020-11-22 05:58

I\'m trying to get self sizing UICollectionViewCells working with Auto Layout, but I can\'t seem to get the cells to size themselves to the content. I\'m having

16条回答
  •  心在旅途
    2020-11-22 06:20

    1. Add flowLayout on viewDidLoad()

      override func viewDidLoad() {
          super.viewDidLoad() 
          if let flowLayout = infoCollection.collectionViewLayout as? UICollectionViewFlowLayout {
              flowLayout.estimatedItemSize = CGSize(width: 1, height:1)
          }
      }
      
    2. Also, set an UIView as mainContainer for your cell and add all required views inside it.

    3. Refer to this awesome, mind-blowing tutorial for further reference: UICollectionView with autosizing cell using autolayout in iOS 9 & 10

提交回复
热议问题