How to set UICollectionViewCell Width and Height programmatically

后端 未结 20 1264
终归单人心
终归单人心 2020-12-04 08:06

I am trying to implement a CollectionView. When I am using Autolayout, my cells won\'t change the size, but their alignment.

Now I would rather want to

20条回答
  •  旧时难觅i
    2020-12-04 08:48

    If, like me, you need to keep your custom flow layout's itemSize dynamically updated based on your collection view's width, you should override your UICollectionViewFlowLayout's prepare() method. Here's a WWDC video demoing this technique.

    class MyLayout: UICollectionViewFlowLayout {
        
        override func prepare() {
            super.prepare()
            
            guard let collectionView = collectionView else { return }
            
            itemSize = CGSize(width: ..., height: ...)
        }
        
    }
    

提交回复
热议问题