gpt4 book ai didi

ios - swift 中的 Collection View 单元格高度和宽度

转载 作者:搜寻专家 更新时间:2023-11-01 05:52:22 25 4
gpt4 key购买 nike

Collection View 有多个图像工作正常。显示是这样的

enter image description here

但是我想这样显示

enter image description here

这是 Collection View 单元格的代码

 let layout: UICollectionViewFlowLayout = UICollectionViewFlowLayout()
layout.sectionInset = UIEdgeInsets(top: 10, left: 0, bottom: 20, right: 0)
let width = UIScreen.main.bounds.width
layout.itemSize = CGSize(width: width/2 , height: width/2 )
layout.minimumInteritemSpacing = 0
layout.minimumLineSpacing = 0
collectionView!.collectionViewLayout = layout

最佳答案

只需实现一些 UICollectionViewDelegateFlowLayout 方法即可获得 UICollectionViewCells 的正确大小和间距。

示例:

func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize
{
let cellSize = CGSize(width: (collectionView.bounds.width - (3 * 10))/2, height: 120)
return cellSize
}

func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumLineSpacingForSectionAt section: Int) -> CGFloat
{
return 10
}

func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, insetForSectionAt section: Int) -> UIEdgeInsets
{
let sectionInset = UIEdgeInsetsMake(10, 10, 10, 10)
return sectionInset
}

只需根据您的要求更改大小和间距值即可。

关于ios - swift 中的 Collection View 单元格高度和宽度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45629220/

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