Reputation: 67
I'm trying to figure out how to save the state of a UIScrollView. For example, I have several images and Labels in a UIScrollView that are loaded from xib.
Now my question is how can I make it when the user returns to this page the state of scrollview is the same that they stopped scrolling at? Does anyone know how to accomplish this? Thanks.
I've found this:
NSUserDefaults *defaults = [NSUserDefaults standardDefaults];
[defaults setFloat: scrollview.contentOffset.x forKey: @"myScrollPositionX"];
[defaults setFloat: scrollview.contentOffset.y forKey: @"myScrollPositionY"];
and for restore:
scrollView.contentOffset = CGPointMake([defaults floatForKey: @"myScrollPositionX"], [defaults floatForKey: @"myScrollPositionY"]);
Where should I implement this? Sorry I'm a newcomer, so pls make it simple :D
Upvotes: 0
Views: 463
Reputation: 2822
You can save the position to the defaults in the viewWillDisappear:animated method of your view controller, and set the content offset in the viewWillAppear:animated method, so the state is saved as the user leaves the view, and is restored just before the user re-enters the view.
Upvotes: 1