How to change UICollectionViewCell size programmatically in Swift?

前端 未结 6 1830
你的背包
你的背包 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:29

    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)
    }
    

提交回复
热议问题