Search code examples
swiftcollectionview

How to preserve collection view scroll position after changing ViewController in SWIFT


The problem is: after scrolling the collectionView and clicking on an item another viewController is presented via navigationController. When I tap on the 'Back' button (top-left) the collection view scrolls to top, but I want it to keep the "scroll position" that it had before changing view controller.

I tried adding
automaticallyAdjustsScrollViewInsets = false

Thank you


Solution

  • automaticallyAdjustsScrollViewInsets has nothing to do with what you are dealing with. The collectionView should not scroll back to top by itself. You are probably calling reloadData() in viewDidAppear or viewWillAppear which triggers the behavior you are describing.