UICollectionView.scrollToItem(at:at:animated:) 在 IOS 17 上将该部分滚动到视图之外

UICollectionView.scrollToItem(at:at:animated:) scroll the section out of view on IOS 17

提问人:Austin Huang 提问时间:11/16/2023 最后编辑:Austin Huang 更新时间:11/17/2023 访问量:78

问:

我发现有时调用会在 IOS 17 设备和带有 Xcode 14 的 Mac OS 15 上滚动到一个奇怪的地方,在 IOS 17 设备(IOS 16...UICollectionView.scrollToItem(at:at:animated:)

下面是一个案例:应用视图

这是我的设置:

private let collectionView = UICollectionView(frame: .zero, collectionViewLayout: UICollectionViewFlowLayout())

view.addSubview(collectionView)
collectionView.snp.makeConstraints { make in
   make.left.right.top.bottom.equalToSuperview()
}

let collectionLayoutConfig = UICollectionViewCompositionalLayoutConfiguration()
collectionLayoutConfig.scrollDirection = .horizontal
let layout = UICollectionViewCompositionalLayout(sectionProvider: { section, _ in
    func getSection(cellCount: Int, extraInterGroupSpacing: Bool = false) -> NSCollectionLayoutSection {
        let cellSpacing = 10
        let cellInset = 24
        let cellLength = UICollectionView.cellLength(cellCount: cellCount, colViewLength: TT.screenWidth,
                                                     cellSpacing: cellSpacing, inset: cellInset)

        let itemSize = NSCollectionLayoutSize(widthDimension: .absolute(cellLength), heightDimension: .absolute(cellLength))
        let item = NSCollectionLayoutItem(layoutSize: itemSize)

        let groupSize = NSCollectionLayoutSize(widthDimension: .fractionalWidth(1), heightDimension: .absolute(cellLength))
        let group = NSCollectionLayoutGroup.horizontal(layoutSize: groupSize, subitems: [item])
        group.contentInsets = NSDirectionalEdgeInsets(top: 0, leading: 0, bottom: 0, trailing: 0)
        group.interItemSpacing = NSCollectionLayoutSpacing.fixed(10)

        let section = NSCollectionLayoutSection(group: group)
        section.contentInsets = NSDirectionalEdgeInsets(top: 10, leading: 24,
                                                        bottom: extraInterGroupSpacing ? 30 : 24,
                                                        trailing: 24)
        section.interGroupSpacing = extraInterGroupSpacing ? 20 : 10
        section.orthogonalScrollingBehavior = .continuous

        return section
    }

    switch section {
    case 0:
        return getSection(cellCount: 7)
    case 1:
        return getSection(cellCount: 4, extraInterGroupSpacing: true)
    default:
        return getSection(cellCount: 4)
    }
}, configuration: collectionLayoutConfig)

collectionView.setCollectionViewLayout(layout, animated: true)
collectionView.showsHorizontalScrollIndicator = false
collectionView.register(cellType: CutoutCell.self)
collectionView.register(cellType: CutoutCellAnimated.self)
collectionView.delegate = self
collectionView.dataSource = self
collectionView.backgroundColor = .clear
collectionView.contentInsetAdjustmentBehavior = .automatic
collectionView.isPagingEnabled = true

我打电话将内容滚动到中心,它过度向上滚动。scrollToItem(at: indexPath, at: .centeredVertically, animated: true)

有没有人遇到过类似的问题?

它不应该过度滚动。

更新: 我发现套装可以解决这个问题。collectionView.isPagingEnabled = false

iOS Swift UIColitalView UIKiting iOS17

评论

0赞 DonMag 11/17/2023
快速测试 - 无法重现您所看到的内容。我们需要更多细节......您的集合视图的框架是什么?您是否涉及任何其他代码,例如设置嵌入?使用您当前的代码,您可以向上拖动以将单元格滚动到视图之外,还是仅在您调用时才会发生这种情况?scrollToItem
0赞 Austin Huang 11/17/2023
感谢您的回复,我已经更新了另一个细节。我可以向上拖动以将单元格滚动到视图之外,但是当我松开手指时,它会向后滚动。

答: 暂无答案