gpt4 book ai didi

ios - Collection View 。 Item 之间的最大距离。 swift 3

转载 作者:行者123 更新时间:2023-11-28 06:05:47 24 4
gpt4 key购买 nike

我知道项目之间只有最小间距,您可以通过尺寸检查器或

func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, insetForSectionAt section: Int) -> UIEdgeInsets

确实在 SE 的模拟器上,我的项目如下图所示。 enter image description here

但是当我在更大的模拟器上运行时,我的单元格项目之间的距离正在增加并且没有按预期显示。

有什么方法可以设置它们之间的最大尺寸吗?

最佳答案

从逻辑上讲,在设置 Collection View 中的项目之间的空间时,您还应该牢记另一个因素,即单元格的大小。如果您按原样获取单元格(使用其默认大小)-我的假设是您的情况-,它将在 Collection View 中静态出队,即单元格的大小将保持原样。

所以,你应该做的是通过符合 UICollectionViewDelegateFlowLayout并实现 collectionView(_:layout:sizeForItemAt:) :

func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
// let's assume that each row in the collection view should contains 3 cells:
let desiredWidth = collectionView.frame.width / 3

// height is also equals to the width to be a square...
return CGSize(width: desiredWidth, height: desiredWidth)
}

此时,如果您将项目之间的最小间距设置为零,则每行应包含三个单元格,它们之间没有空格。

如果你的目标是在单元格之间设置一个小空间,你应该在声明单元格所需的宽度时反射(reflect)它,例如:

// since each row contains 3 cells, subtracting 2 from each cell width
// means it would be 6 points (3 * 2) divided at the whole row (2 spaces, 3 points for each)
let desiredWidth = collectionView.frame.width / 3 - 2

关于ios - Collection View 。 Item 之间的最大距离。 swift 3,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48263086/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com