swift - selectItem 时的集合视图布局错误(Swift 5)
问题描述
从 viewDidLoad 布局中选择单元格后更改
在选择单元格之前
选择单元格后
let indexPath = IndexPath(row: SelectedFolderIndex, section: 0)
collectionView.selectItem(at: indexPath, animated: false, scrollPosition: .centeredVertically)
布局
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
return CGSize(width: collectionView.frame.width/2.2, height: 55)
}
ViewDidLoad
let layout: UICollectionViewFlowLayout = UICollectionViewFlowLayout()
layout.sectionInset = UIEdgeInsets(top: 7, left: 12, bottom: 12, right: 12)
layout.minimumInteritemSpacing = 5
layout.minimumLineSpacing = 11
collectionView!.collectionViewLayout = layout
collectionView.delegate = self
collectionView.dataSource = self
collectionView.allowsMultipleSelection = false
解决方案
您需要从单元格宽度中剪切插图和间距:
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
let width = (collectionView.frame.width/2.2) - 2*12 - 11
return CGSize(width: width, height: 55)
}
推荐阅读
- python - 如何在 Python 中读取多行输入?
- recursion - Clojure:你能在递归函数中连接吗?
- constructor - NewFeedModel 的构造函数不能应用于给定类型
- r - 如何找到低于0值的5行或更多行(日期)系列的第一行(日期)
- amazon-web-services - 更新 Glue Table Schema 时设置小数位数
- sql-server - 执行 SSIS 任务需要太长时间才能验证
- mysql - 学生支付的总费用
- python - “int”对象没有属性“get_length”错误
- c# - 阻止角色跳跃高度根据帧速率变化?
- elasticsearch - 日期范围查询不适用于弹性 2.4 集群