UIScrollView's origin changes after popping back to the UIViewController
Solution 1
Actually, I put that line of code in viewDidDisappear
, and so that it remembers the offset when the view reappears, I added this line before it
self.contentOffset = self.scrollView.contentOffset;
as well as
- (void)viewDidLayoutSubviews {
self.scrollView.contentOffset = self.contentOffset;
}
Solution 2
In iOS 7/8/9 simple self.automaticallyAdjustsScrollViewInsets = NO;
solved the problem in my case.
Solution 3
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.
Solution 4
i had a similar problem, after dismissing a viewController, the contentOffset from my tableView was changed to (0, -64).
my solution was a little weird, i tried all the other answers but had no success, the only thing that fixed my problem was to switch the tableView position in the controls tree of the .xib
it was the first control in the parent View like this:
I moved the tableView right after the ImageView and it worked:
it seems that putting the table view in the first position was causing the trouble, and moving the table view to another position fixed the problem.
P.D. I'm not using autoLayout neither storyboards
hope this can help someone!
Solution 5
I'm using a collectionView and I had a similar problem. For iOS 11: in the size inspector, there is "content inset". Set that to "Never". That solved the problem for me. I hope this helps someone.
Objective C:
if (@available(iOS 11, *)) {
[UIScrollView appearance].contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
}
Swift:
if #available(iOS 11, *) {
UIScrollView.appearance().contentInsetAdjustmentBehavior = .never
}
Peter Jacobs
Updated on July 09, 2022Comments
-
Peter Jacobs almost 2 years
I have a
UIViewController
subclass as a scene in the storyboard that contains aUIScrollView
containing various subviews. One of the subviews is aUIButton
which segues into another sceneUIViewController
subclass. When I come back from the view (pop theUIViewController
off the navigation controller stack), I find that the scroll view's origin has somehow changed, although thecontentsize
andcontentoffset
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?