How to set UICollectionViewCell Width and Height programmatically

后端 未结 20 1262
终归单人心
终归单人心 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条回答
  •  执念已碎
    2020-12-04 08:58

    Swift 5 Programmatically

    lazy var collectionView: UICollectionView = {
            let layout = UICollectionViewFlowLayout()
            layout.scrollDirection = .horizontal
    
            //Provide Width and Height According to your need
            let cellWidth = UIScreen.main.bounds.width / 10
            let cellHeight = UIScreen.main.bounds.height / 10
            layout.itemSize = CGSize(width: cellWidth, height: cellHeight)
    
            //You can also provide estimated Height and Width
            layout.estimatedItemSize = CGSize(width: cellWidth, height: cellHeight)
    
            //For Setting the Spacing between cells
            layout.minimumInteritemSpacing = 0
            layout.minimumLineSpacing = 0
    
            return UICollectionView(frame: self.view.frame, collectionViewLayout: layout)
        }()
    

提交回复
热议问题