gpt4 book ai didi

ios - Swift 3 - 带填充的 UICollectionView 中心单元格

转载 作者:可可西里 更新时间:2023-11-01 01:26:09 25 4
gpt4 key购买 nike

我有一个水平滚动的 UICollectionView,我希望一次在屏幕上显示一个单元格。

我通过以下方法以编程方式设置单元格宽度和高度:

func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "Cell", for: indexPath as IndexPath) as! Cell

let width = collectionView.bounds.size.width - 40
let height = collectionView.bounds.size.height - 40

cell.bounds.size = CGSize(width: CGFloat(width), height: CGFloat(height))

return cell
}

在努力弄清楚如何使用插图、间距、节插图(以编程方式和在 XCode 的检查器中)后,我似乎无法弄清楚我需要设置什么以及我需要在哪里设置它。

这是我想要的间距图: enter image description here

最佳答案

func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAtIndexPath indexPath: IndexPath) -> CGSize {
return CGSize(width: UIScreen.main.bounds.width - 40, height: yourCollectionViewHeight - 40)
}

left 20 - 在 Storyboard中添加 Footer SizeHeader Sizewidth = 20

单元格之间的间距 - 在 Storyboard中添加 Minimum Spacing = 40

enter image description here

我可能犯了一些错误,您不会得到准确的布局,但我希望您能理解。

关于ios - Swift 3 - 带填充的 UICollectionView 中心单元格,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41453857/

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