Reputation: 42670
Every-time an UIViewController
swiped away from current page, it will be destroyed.
If the UIViewController
swiped back again as current page, it will be re-created again.
However, as you can see from the screen recording, the scroll position of UICollectionView
is "lost" every-time we swipe back as current page.
I was wondering, what are some good ways, to retain mutiple UICollectionView's scroll position within a UIPageViewController?
UICollectionView
in a global variable? viewDidUnload
seems not being called anymore.viewDidLoad
?Thanks.
Upvotes: 1
Views: 89
Reputation: 4239
- Does iOS provide any underlying framework for us to achieve so?
Not really, this will need to be handled manually. Let's say you would introduce some kind of delegate protocol that would be implemented by the root controller.
protocol ScrollPositionDelegate: class {
func collectionView(_ collectionView: UICollectionView, didChangeScrollPosition point: CGPoint)
}
Then all of the child views would keep weak reference to the delegate
weak var delegate: ScrollPositionDelegate?
And call it whenever the position changes. In root You could identify the which view called the delegate method by using tag
property on the collectionView (you would need to set it first within the child screen)
extension RootViewController: ScrollPositionDelegate {
func collectionView(_ collectionView: UICollectionView, didChangeScrollPosition point: CGPoint) {
switch collectionView.tag {
case 1:
// store scroll position for screen 1
default:
return
}
// or simply
scrollPosition[collectionView.tag] = point
// obviously you would need storage like `scrollPosition: [Int: CGPoint]`
}
}
- If we need to do it manually, when we should save the scroll position of UICollectionView in a global variable? viewDidUnload seems not being called anymore.
You can either save the values when the viewWillDisappear
or when the collection view delegate calls scrollViewDidScroll
- When is the good time to restore the scroll position? viewDidLoad?
viewDidLoad
is good idea to start but there is no guarantee that the layout will be ready. You could try again atviewWillAppear
or after you callcollectionView.reloadData()
Upvotes: 1