How to adjust height of UICollectionView to be the height of the content size of the UICollectionView?

后端 未结 14 1114
死守一世寂寞
死守一世寂寞 2020-11-28 03:51

I would like the UICollectionView (The red one) to shrink to the height of the content size in this case UICollectionViewCells(the yellow ones) because there is a lot of emp

14条回答
  •  Happy的楠姐
    2020-11-28 04:30

    If you set the height constraint of the collection view. Just observe the contentSize change in the viewDidLoad and update the constraint.

            self.contentSizeObservation = collectionView.observe(\.contentSize, options: [.initial, .new]) { [weak self] collectionView, change in
                guard let `self` = self else { return }
                guard self.collectionView.contentSize != .zero else { return }
                self.collectionViewHeightLayoutConstraint.constant = self.collectionView.contentSize.height
            }
    

提交回复
热议问题