ios - 自动调整collectionview单元格不能以最小的行空间正确显示
问题描述
我有自动调整单元格集合视图,我需要在其中水平显示数据。一切正常,但我需要在两个单元格之间放置 5px 间距,因此minimumLineSpacing
属性和问题开始了我的最后一个单元格由于行间距而无法正确显示。这是图像
这是代码
extension TopicVC : UICollectionViewDelegate,UICollectionViewDataSource,UICollectionViewDelegateFlowLayout
{
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return 5
}
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumLineSpacingForSectionAt section: Int) -> CGFloat {
return 5.0
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: HastagCell.identifier, for: indexPath) as! HastagCell
let index = indexPath.row % arrClrFollowTopic.count
cell.btnHashTag.backgroundColor = Color.custom(hexString: arrClrFollowTopic[index], alpha: 1.0).value
if indexPath.row % 3 == 0
{
cell.btnHashTag.setTitle(strTitle: "#Testing 123545")
}
else
{
cell.btnHashTag.setTitle(strTitle: "#Testing")
}
return cell;
}
func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
}
}
ViewDidLoad
override func viewDidLoad() {
super.viewDidLoad()
self.objCollectionView.register(HastagCell.nib, forCellWithReuseIdentifier: HastagCell.identifier)
if let collectionViewLayout = self.objCollectionView.collectionViewLayout as? UICollectionViewFlowLayout{
collectionViewLayout.estimatedItemSize = CGSize.init(width: 1.0, height: 1.0)
}
self.objCollectionView.delegate = self
self.objCollectionView.dataSource = self
// Do any additional setup after loading the view.
}
带有自动布局的我的单元格设计
集合视图布局设计
解决方案
您需要将布局的 minimumInteritemSpacing 值设置为与 minimumLineSpacing 相同。
所以添加:
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumInteritemSpacingForSectionAt section: Int) -> CGFloat {
return 5
}
推荐阅读
- java - spring boot rest-api PUT 请求返回空值
- performance - J Meter - 移动应用程序录制 - SSL HandshakeException
- python - 如何检测树莓派板子型号?
- javascript - 使用图像数据处理后获取变换和缩放坐标
- r-markdown - 在 Bookdown (gitbook) 中通过“参考书目”更改“参考”
- paypal - PayPal Checkout 控制台错误:无法在“购物车”类型上查询“补充”字段
- r - 我如何告诉 R 包 Limma 在 read.idat() 中使用什么作为“目标”?
- node.js - 在 Docker 构建中找不到 TSC
- javascript - 如何使用 jQuery 遍历 div、抓取 href 和包装不同的元素?
- python-3.6 - 我已经安装了 python-3.9.1 和 pip 但是在运行命令 python3 -V 得到结果为 python 3.6.9 如何获得 python 版本 3.9