UICollectionView Self Sizing Cells with Auto Layout

前端 未结 16 1757
野性不改
野性不改 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:13

    In iOS 10+ this is a very simple 2 step process.

    1. Ensure that all your cell contents are placed within a single UIView (or inside a descendant of UIView like UIStackView which simplifies autolayout a lot). Just like with dynamically resizing UITableViewCells, the whole view hierarchy needs to have constraints configured, from the outermost container to the innermost view. That includes constraints between the UICollectionViewCell and the immediate childview

    2. Instruct the flowlayout of your UICollectionView to size automatically

      yourFlowLayout.estimatedItemSize = UICollectionViewFlowLayout.automaticSize
      

提交回复
热议问题