我创建了collectionView,我有3个细胞。我想让我的手机看起来像这样,所有设备的屏幕都是这样:

但我有iPad的结果。

怎么修呢?
更新
我在collectionView中添加了UIViewController。对于collectionView,我有这样的限制:

给我的细胞打量:

代码:
func numberOfSections(in collectionView: UICollectionView) -> Int {
return 4
}
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return 3
}
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
let itemsPerRow: CGFloat = 3
let itemWidth = (collectionView.bounds.width / itemsPerRow)
let itemHeight = itemWidth * 1.5
return CGSize(width: itemWidth, height: itemHeight)
}发布于 2018-07-01 21:27:31
您需要自适应地更改单元格大小,否则大小不会适用于所有屏幕大小。
您有自己的函数,在其中返回单元格大小:
return CGSize(width: 182, height: 275)相反,让它像这样适应:
return CGSize(width: self.view.frame.width / 3, height: self.view.frame.height)或者,若要在两者之间存在差距,则创建一个抵消:
let offset = 10
let width = self.view.frame.width - offset * 4 // 4 gaps
let height = self.view.frame.height - offset * 2 // 2 gaps
return CGSize(width: width / 3, height: height) // Divide into equally-sized cells当然,可以将self.view替换为您希望单元格适合的任何容器。
https://stackoverflow.com/questions/51121988
复制相似问题