How to set UICollectionViewCell Width and Height programmatically

后端 未结 20 1215
终归单人心
终归单人心 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 09:03

    Try to use UICollectionViewDelegateFlowLayout method. In Xcode 11 or later, you need to set Estimate Size to none from storyboard.

    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: 
    UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
        let padding: CGFloat =  170
        let collectionViewSize = advertCollectionView.frame.size.width - padding
        return CGSize(width: collectionViewSize/2, height: collectionViewSize/2)
    }
    

提交回复
热议问题