Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

UIScrollView's origin changes after popping back to the UIViewController

I have a UIViewController subclass as a scene in the storyboard that contains a UIScrollView containing various subviews. One of the subviews is a UIButton which segues into another scene UIViewController subclass. When I come back from the view (pop the UIViewController off the navigation controller stack), I find that the scroll view's origin has somehow changed, although the contentsize and contentoffset seem correct.

What's also interesting is that the app has a tab bar, and when I tab away and back to that view, the scroll view is set back correctly with offset at (0, 0).

There is basically no code involved in this process, as it's pretty much all in the storyboard. As I am fairly new to using the storyboard, I figure I'm doing something wrong, although I don't know what. Any ideas as to what that may be? Perhaps sizing issues or constraints?

like image 400
Peter Jacobs Avatar asked Jul 01 '13 12:07

Peter Jacobs


2 Answers

In iOS 7/8/9 simple self.automaticallyAdjustsScrollViewInsets = NO; solved the problem in my case.

like image 108
Alex Avatar answered Oct 12 '22 18:10

Alex


Try this in viewWillAppear of the view controller you pop back into:

 self.scrollView.contentOffset = CGPointMake(0, 0); 

Edit: When also adding Peter's code you get the best results with:

- (void)viewWillAppear:(BOOL)animated {     [super viewWillAppear:YES];     self.scrollView.contentOffset = CGPointMake(0, 0); } 

plus

- (void)viewWillDisappear:(BOOL)animated {       self.recentContentOffset = self.scrollView.contentOffset;     [super viewWillDisappear:animated]; } 

and

- (void)viewDidLayoutSubviews {     [super viewDidLayoutSubviews];     self.scrollView.contentOffset = CGPointMake(0, self.recentContentOffset.y); } 

You return to the original scroll position, have no visual side-effect and the scroll view itself is correctly positioned in its superview (which was a problem) after coming back.

like image 21
MacMark Avatar answered Oct 12 '22 19:10

MacMark