Auto Layout Collection view according to various s

2019-07-23 16:36发布

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 enter image description here

How it should look enter image description here

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

1条回答
\"骚年 ilove
2楼-- · 2019-07-23 17:13

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.

查看更多
登录 后发表回答