具有固定行数和列数的集合视图

Use*_*ser 3 flowlayout collectionview ios swift

我有一个集合视图,并希望在多个设备上保持一致的行数和列数.在iPhone 6s Plus上,会显示6个项目,但在iPhone 5s上查看应用程序时,仅显示四个项目.

在这里查看我的布局: iPhone 6s Plus上的布局

如何使集合视图在各种设备上显示相同的项目?

Rik*_*ikh 6

对于Swift 3.0.

确保您使用autoLayout并实现UICollectionView sizeForItemAtIndexPath.

  func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAtIndexPath indexPath: NSIndexPath) -> CGSize {

        return CGSize(width: collectionView.bounds.size.width/3 - yourCellInterItemSpacing, height: collectionView.bounds.size.height/3 - yourCellInterItemSpacing)
  }
Run Code Online (Sandbox Code Playgroud)

这将允许根据屏幕大小创建单元格(假设您使用的是autoLayout),您应该除以所需的列数/行数.将宽度除以所需的列数和高度除以行数.

要提供屏幕截图中显示的前导和尾随空格,只需在故事板中添加适当的空格即可UICollectionView.即领先和尾随空格相当于yourCellInterItemSpacing

确保您的班级符合UICollectionViewDelegateFlowLayout协议!