gpt4 book ai didi

ios - UICollectionView:ScrollToItem 到中心单元格在水平 Collection View 上不起作用

转载 作者:搜寻专家 更新时间:2023-10-31 22:17:58 24 4
gpt4 key购买 nike

我有一个带有内容偏移量的水平 UICollectionView,这样每个元素(包括最左边的项目)都可以滚动到中心:

let cvOffset = (UIScreen.main.bounds.width - tileSize) / 2
collectionView.contentInset = UIEdgeInsets(top: 0, left: cvOffset, bottom: 0, right: cvOffset)
collectionView.scrollIndicatorInsets = UIEdgeInsets(top: 0, left: cvOffset, bottom: 0, right: cvOffset)

但是,当我使用 scrollToItem 方法时,它只对右半部分的项目有效。如果我选择左半部分的任何项目,则会输入第一个单元格。你知道为什么吗?

collectionView.scrollToItem(at: IndexPath.init(row: sender.tag, section: 0), at: .centeredHorizontally, animated: true)

最佳答案

不确定这是具有流式布局的 UICollectionView 中的错误还是某种期望的结果,但您可以通过设置 UICollectionViewFlowLayout 来完成您需要的操作 sectionInset 到您的内容插入,然后 scrollToItem 将按预期工作:

因此删除您的 contentInset 并将以下内容添加到您的布局中:

layout.sectionInset = UIEdgeInsets(top: 0, left: cvOffset, bottom: 0, right: cvOffset)

在我的测试应用程序中,它做了它应该做的,所以滚动到选定的单元格并以所有 indexPaths 为中心

关于ios - UICollectionView:ScrollToItem 到中心单元格在水平 Collection View 上不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50231998/

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