PGDev
PGDev

Reputation: 24341

UICollectionView's contentOffset changed on reload

I'm working UICollectionView and facing issue with contentOffset while reloading the data.

I already read multiple posts regarding this, but couldn't find any solution.

Issue:

UINavigationController -> ViewController1 (rootViewController)

ViewController1 contains a vertical UICollectionView that contains multiple UICollectionViewCells.

On tapping a particular UICollectionViewCell, ViewController2 is pushed into the navigation stack.

Now, on returning back to ViewController1, I'm reloading the UICollectionView which changes the previous contentOffset.

What is required?

I don't want the contentOffset of UICollectionView to be changed when I return back to ViewController1 from ViewController2.

Upvotes: 2

Views: 1812

Answers (1)

Shezad
Shezad

Reputation: 756

try this code for reloading your collectionView:

let contentOffset = collectionView.contentOffset
    collectionView.reloadData()
    collectionView.layoutIfNeeded()
    collectionView.setContentOffset(contentOffset, animated: false)

Upvotes: 2

Related Questions