Reputation: 157
In my VC, I've table view. I want to remember the exact scroll position so that when user comes back to this view I can scroll to the same position. My requirement is that I've to reload data when user comes back to view.
I've tried using content offset method but this offset is going way off the table height so this solution doesn't work for me.
I've tried saving indexPath and scrolling to this position but this doesn't give exact position. I've to specify middle, top or bottom which is not exact position.
So how do we remember exact scroll position and scroll to that position after reloading data in table?
self.tableView.scrollToRow(at: scrollToSavedIndexPath, at: UITableView.ScrollPosition.middle, animated: false)
Upvotes: 1
Views: 988
Reputation: 1630
QUESTION: I just saw that you mentioned that contentOffset wasn't working for you. Can you try the method again by printing out the contentOffset value each time? That's a good starting point to understand why the offset method is not working for you.
Use the following to save the scroll position:
let offset = tableView.contentOffset
Then set the scroll position as follows:
tableView.setContentOffset(offset, animated: false)
Upvotes: 1