0

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

j3141592653589793238
  • 1,810
  • 2
  • 16
  • 38
  • What code do you have in the `didSelectItemAtIndexPath` function? – Jake Jun 18 '18 at 13:10
  • Nothing @Jake. I just have a custom `inputAccessoryView` that is placed on the bottom of the screen. – j3141592653589793238 Jun 18 '18 at 13:51
  • Do you have your constraints set? – Jake Jun 18 '18 at 13:54
  • For the inputAccessoryView? I did. I added an image to my answer to make clear how my view controller looks like. @Jake This might also be related to https://stackoverflow.com/questions/34081218/uicollectionview-is-scrolling-when-selecting-a-cell. However, I don't select cells programmatically here. – j3141592653589793238 Jun 18 '18 at 13:58
  • I think that you may be experiencing a constraints issue between the two views. Do you know how to use the Debug View Hierarchy ? I would analyze the views and margins before and after a cell click using that tool to see what is occurring. – Jake Jun 18 '18 at 14:06
  • I am not that experienced using it..but I will find it out. Are you talking about the constraints between the `inputAccessoryView` and the `collectionView`? – j3141592653589793238 Jun 18 '18 at 14:09
  • Yes. Something may be acting in an unexpected way – Jake Jun 18 '18 at 14:29
  • Does not seem to be the case, as I didn't even set constraints on the `inputAccessoryView` , I just set its frame. The `inputAccessoryView` is not even visible in the View Hierarchy Debugger. – j3141592653589793238 Jun 18 '18 at 14:34
  • Try setting a constraint on the collection view's bottom margin to the top of the inputAccessoryView – Jake Jun 18 '18 at 14:46
  • That doesn't work because the `inputAccessoryView` is not added within `viewDidLoad()` but by overriding the `inputAccessoryView` property. – j3141592653589793238 Jun 18 '18 at 14:52
  • 1
    Access the height of the `inputAccessoryView` and set the bottom constraint of the `CollectionView` to the bottom of the view offset by the height of the `inputAccessoryView` That should solve your problem. – Jake Jun 18 '18 at 14:55
  • @Jake Woohoo! I really spent way too much time and effort in solving this minor issue! I'd hug you if I could. Unfortunately, I couldn't change the Autolayout constraints of the `UICollectionView`, but I could change its frame (more precisely, its height) and decrease it by the height of the `inputAccessoryView`. That works like a charm. Awesome. If you'd like to answer this question; I'd accept your answer. Thanks a lot! – j3141592653589793238 Jun 18 '18 at 15:49
  • 1
    Thanks @T.Meyer happy to help. – Jake Jun 18 '18 at 16:36

2 Answers2

1

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.

Jake
  • 2,126
  • 1
  • 10
  • 23
1

In my case it happens with on collection view when you have paging enabled selected. Just deselect paging enabled and it should be alright.

See attached image

Helton Malambane
  • 1,147
  • 11
  • 12