gpt4 book ai didi

ios - 如何在 swift 4 的中心显示 CollectionViewCell

转载 作者:行者123 更新时间:2023-11-29 11:29:48 26 4
gpt4 key购买 nike

Collectionview 单元格在一行中有两个项目,但 Collectionview 单元格在我的设计中没有居中。

enter image description here

如何以居中对齐方式显示 collectionViewCell?我要显示如下图

enter image description here

试了很多次,都无法显示CollectionView的中心。请帮助我!

最佳答案

工作代码 Swift 4 | swift 5

你需要像这样使用UICollectionViewDelegateFlowLayout方法

class CardListViewController:UICollectionViewDelegate,UICollectionViewDataSource,UICollectionViewDelegateFlowLayout{

// UICollectionViewDelegateFlowLayout Delegate method

func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize
{
let leftAndRightPaddings: CGFloat = 45.0
let numberOfItemsPerRow: CGFloat = 2.0

let width = (collectionView.frame.width-leftAndRightPaddings)/numberOfItemsPerRow
return CGSize(width: width, height: width) // You can change width and height here as pr your requirement

}
}

输出:

enter image description here

关于ios - 如何在 swift 4 的中心显示 CollectionViewCell,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54672069/

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