How to set UICollectionViewCell Width and Height programmatically

后端 未结 20 1033
终归单人心
终归单人心 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:11

    Make sure to add the protocol UICollectionViewDelegateFlowLayout in your class declaration

    class MyCollectionViewController: UICollectionViewController, UICollectionViewDelegateFlowLayout
    {
        //MARK: - UICollectionViewDelegateFlowLayout
    
        func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize
        {
           return CGSize(width: 100.0, height: 100.0)
        }
    }
    
    0 讨论(0)
  • 2020-12-04 09:12

    Try below method

    func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAtIndexPath indexPath: NSIndexPath) -> CGSize {
        return CGSize(width: 100.0, height: 100.0)
    }
    
    0 讨论(0)
提交回复
热议问题