UICollectionView autosize height

前端 未结 9 1729
孤城傲影
孤城傲影 2020-12-04 07:02

How do I properly resize a UICollectionView so that it fully displays its contents? I have tried many things, including setting its frame, calling reloadData an

9条回答
  •  刺人心
    刺人心 (楼主)
    2020-12-04 07:27

    Here's a way to bind the CollectionView's height via it's intrinsic size. I used it to properly size a CollectionView inside a TableView Cell (with dynamic cells height). and it works perfectly.

    First, add this to your UICollectionView subclass:

    override var intrinsicContentSize: CGSize {
        get {
            return self.contentSize
        }
    }
    

    Then call layoutIfNeeded() after you reload data:

    reloadData()
    layoutIfNeeded()
    

提交回复
热议问题