Variable width & height of UICollectionViewCell using AutoLayout with Storyboard (without XIB)

后端 未结 3 2323
长情又很酷
长情又很酷 2021-02-19 19:05

Here is an design issue in the app that uses AutoLayout, UICollectionView and UICollectionViewCell that has automatically resizable width & height depending on AutoLayout co

3条回答
  •  余生分开走
    2021-02-19 19:59

    If you are using constraints, you don't need to set a size per cell. So you should remove your delegate method func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAtIndexPath indexPath: NSIndexPath) -> CGSize

    Instead you need to set a size in estimatedItemSize by setting this to a value other than CGSizeZero you are telling the layout that you don't know the exact size yet. The layout will then ask each cell for it's size and it should be calculated when it's required.

    let layout = collectionView.collectionViewLayout as! UICollectionViewFlowLayout
    layout.estimatedItemSize = someReasonableEstimatedSize()
    

提交回复
热议问题