根据各种屏幕尺寸自动布局收集视图

我正在使用集合视图委托和数据源在普通视图控制器中使用集合视图。 我正在使用sizeForItemAtIndexPath,但它没有调整单元格的大小。

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

正确使用autoLayout为集合视图分配约束。

它在iPhone5中的外观如何 在此处输入图像描述

应该怎么样 在此处输入图像描述

我知道使用了estimatedItemSize,但我无法使用它,因为这不是一个UICollectionViewController。 谢谢你的任何建议?

确保:

  • 确认到UICollectionViewDelegateFlowLayout
  • collectionView的宽度=屏幕的宽度。
  • 单元格的最小间距为零。 您可以从Interface Builder(选择collectionView – > show size inspector – > set min spacing to 0)或通过实现minimumInteritemSpacingForSectionAt返回零来更改它。

我希望能回答你的问题。