具有固定行数和列数的集合视图 [英] Collection View With Fixed Number of Rows and Columns

查看:105
本文介绍了具有固定行数和列数的集合视图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have a collection view and would like to maintain a consistent number of rows and columns across multiple devices. On an iPhone 6s Plus, 6 items are displayed, but when viewing the app on an iPhone 5s, only four items are displayed.

在这里查看我的布局:

See my layout here:

如何制作收藏夹视图在各种设备上显示相同的项目?

How can I make the collection view display the same items on various devices?

推荐答案

对于Swift 3.0。

确保您正在使用autoLayout并实现 UICollectionView sizeForItemAtIndexPath

Ensure you are using autoLayout and implement the 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)
  }

这将允许您根据屏幕尺寸创建单元格(假设您使用的是autoLayout),则应除以您想要的列数/行数。将宽度除以所需的列数,将高度除以行数。

This will allow your cell to be created based on the screen size (Assuming you are using autoLayout), you should divide by the number of columns/rows you want. Divide width by the number of columns you want and height by number of rows.

要提供屏幕快照中所示的前导和尾随空格,只需在情节提要中添加适当的空格即可。到您的 UICollectionView 。即等于和 yourCellInterItemSpacing

To provide leading and trailing spaces as seen in your screenshot, just add appropriate spaces in the storyboard to your UICollectionView. I.e Leading and trailing spaces equivalent to yourCellInterItemSpacing

的开头和结尾空格确保您的类符合 UICollectionViewDelegateFlowLayout 协议!

Make sure your class conforms to the UICollectionViewDelegateFlowLayout protocol!

这篇关于具有固定行数和列数的集合视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆