How to change UICollectionViewCell size programmatically in Swift?

前端 未结 6 1847
你的背包
你的背包 2020-12-07 20:09

I have a UICollectionView with a segmented control to switch between data. But how do I change the UICollectionViewCell size propertie

6条回答
  •  情书的邮戳
    2020-12-07 20:49

    Swift 3

    Methods from @sumit-oberoi answer won't be called if you are using Swift 3. To make it work make this adjustments:

    1. Conform to the UICollectionViewDelegateFlowLayout protocol.

    2. Implement these methods:

      func collectionView(_ collectionView: UICollectionView,
                          layout collectionViewLayout: UICollectionViewLayout,
                          sizeForItemAt indexPath: IndexPath) -> CGSize {
      
      }
      
      func collectionView(_ collectionView: UICollectionView,
                          layout collectionViewLayout: UICollectionViewLayout,
                          minimumInteritemSpacingForSectionAt section: Int) -> CGFloat {
          return 1.0
      }
      
      func collectionView(_ collectionView: UICollectionView, layout
          collectionViewLayout: UICollectionViewLayout,
                          minimumLineSpacingForSectionAt section: Int) -> CGFloat {
          return 1.0
      }
      

    Notice the changes:

    1. add _ before collectionView in the method name
    2. NSIndexPath changes to IndexPath

提交回复
热议问题