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

后端 未结 14 1154
死守一世寂寞
死守一世寂寞 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条回答
  •  盖世英雄少女心
    2020-11-28 04:18

    On your UICollectionView set your constraints such as Trailing, Leading, and Bottom:

    If you look at my height constraint in more detail, as it is purely for storyboard look so I don't get errors, I have it to Remove at build time. The real height constraint is set in my code down below.

    My code for DrawerCollectionView, which is set as the collection view Custom Class:

    import UIKit
    
    class DrawerCollectionView: UICollectionView {
    
        override func didMoveToSuperview() {
            super.didMoveToSuperview()
    
            heightAnchor.constraint(equalToConstant: contentSize.height).isActive = true
        }
    }
    

提交回复
热议问题