为什么我的 UICollectionView 在单击单元格时会向上滚动?

Why does my UICollectionView scroll up when clicking on a cell?

我的 UICollectionView 在单击 UICollectionViewCell 时总是向上滚动一点。
我注意到,如果我将集合视图插图设置为 0(根本没有插图)并将 contentInsetAdjustmentBehavior 设置为 .never,则不会发生此行为。

但是,我仍然希望在我的 UICollectionView 上插入内容(底部和顶部各 8 像素)而无需自动滚动。我不确定为什么会出现这种行为以及它与内容插图 属性 究竟有什么关系。

有没有办法让自定义插图在点击 UICollectionViewCell 时不会导致滚动?

这是我在 viewDidLoad() 中的配置:

self.collectionView?.contentInset = UIEdgeInsets(top: 8, left: 0, bottom: 8, right: 0)
// self.collectionView?.contentInsetAdjustmentBehavior = .never
// When setting the contentInset to zero and the contentInsetAdjustmentBehavior to never, it does not scroll

VC 看起来像这样:

每个单元格代表一条消息。底部的视图是 inputAccessoryView.

当我删除 inputAccessoryView 时,该行为不会发生。 这也可能与此讨论有关:https://forums.developer.apple.com/thread/18424

Answer - Read the comments

我怀疑是布局问题。要解决此问题,需要静态设置布局,这样它在交互时不会调整。这通常可以通过约束来完成,在这种情况下,设置 CollectionView 偏移 inputAccessoryView 的高度就是答案。

在我的例子中,当您选择了 启用分页 时,它发生在集合视图上。只需取消选择 启用分页 就可以了。