Auto Layout Collection view according to various screen sizes

大城市里の小女人 提交于 2020-01-05 05:28:05

问题


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.



来源:https://stackoverflow.com/questions/39944207/auto-layout-collection-view-according-to-various-screen-sizes

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!