iosswiftscrolluicollectionviewuiedgeinsets

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


My UICollectionView always scrolls a bit up when clicking on a UICollectionViewCell.
I noticed that this behavior does not occur if I set the collection view inset to 0 (no inset at all) and the contentInsetAdjustmentBehavior to .never.

However, I still want to have a content inset (of 8px on bottom and top) on my UICollectionView without that automated scrolling. I am not sure why this behavior occurs and what exactly it has to do with the content inset property.

Is there a way to have a custom inset that does not lead to scrolling whenever a UICollectionViewCell is tapped?

This is my configuration within 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

The VC looks like this:

Every cell represents a message. The view at the bottom is an inputAccessoryView.

The behavior does not occur when I remove the inputAccessoryView. This might also be related to this discussion: https://forums.developer.apple.com/thread/18424


Solution

  • Answer - Read the comments

    My suspicion was layout issues. To correct the issue the layout needs to be statically set, that way it doesn't adjust when it's interacted with. That could usually be done with constraints, in this circumstance setting the height of the CollectionView offset by the inputAccessoryView was the answer.