swift - 无论我做什么,CollectionView Cells 都不会在视图中居中
问题描述
我无法将我的 CollectionView 单元格集中在 Collection View 上。这就是我现在在 Stackoverflow 上找到的(但尝试了一些其他不起作用的解决方案):
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, insetForSectionAt section: Int) -> UIEdgeInsets {
let totalCellWidth = cellWidth * CGFloat(posts.count)
let totalSpacingWidth = CGFloat(cellSpacing * (posts.count - 1))
let collectionViewWidth = collectionView.frame.width
let leftInset = (collectionViewWidth - (totalCellWidth + totalSpacingWidth)) / 2
let rightInset = leftInset
return UIEdgeInsets(top: 0, left: leftInset, bottom: 0, right: rightInset)
}
我尝试编写自己的解决方案,但没有任何效果。
解决方案
推荐阅读
- javascript - AngularJS 1.8 和 ES6 模块:如何创建“通过”基于类的 API 接口的服务或工厂?
- python - Seaborn 绘图-Python
- linux - 在 ubuntu 上的 hyper.is 中更改默认开始位置
- python - 从文件中保存和读取 kwargs
- django - Django按多列分组并获取唯一组合的计数
- algorithm - 基于属性的算法生成器测试:“找到给定序列中不出现的最小正整数”
- excel - 您可以引用具有可变地址的 Excel 单元格吗?
- java - Java桌面客户端nodejs服务器使用socketio,socket未连接
- python - 将字符串的 TFIDF 相似度计算为 2 个 Pandas 列,并在新列中输出相似度得分
- react-native - WatermelonDB w ReactNative:如何在模型中正确使用@actions?