-->

Auto Layout Collection view according to various s

2019-07-23 16:39发布

问题:

I'm using a collection view inside a normal view controller using the collection view delegate and data source. I'm using the sizeForItemAtIndexPath but it doesn't resize the cell.

let screenSize: CGRect = UIScreen.main.bounds

func collectionView(_collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAtIndexPath indexPath: NSIndexPath) -> CGSize
{
    return CGSize(width: screenSize.width / 3, height: screenSize.width / 3)
}

The collection view is assigned the constraints using autoLayout properly.

How it looks in iPhone5

How it should look

I know the estimatedItemSize is used but I'm unable to use it since this is not a UICollectionViewController. Thanks for any suggestion?

回答1:

Make sure that:

  • Confirming to UICollectionViewDelegateFlowLayout.
  • collectionView's width = screen's width.
  • Min Spacing for cells is zero. You can change it from the Interface Builder (Select the collectionView -> show size inspector -> set min spacing to 0), or by implementing minimumInteritemSpacingForSectionAt returning zero.

I hope that answered your question.