UIScrollView contentOffset change after another view pushed

19,176

Solution 1

See my answer to a similar question.

You need to set the scrollview's contentOffset appropriately in viewWillAppear: and viewWillDisappear:.

Also, see this:

Solution 2

For iOS 7/8 self.automaticallyAdjustsScrollViewInsets = NO; solved the problem.

Solution 3

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:

before

I moved the tableView right after the ImageView and it worked:

after

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 4

When you leave the screen where the scrollview is present and come back, it will add half of the size of the screen on top or left or both. A blank space, something you don't want. And in my case, the paging becomes a mess and doesn't work properly, until I put the scroll in the CGPointZero programatically or by touch, as an user. If you call NSLog( @"%f", scrollview.contentSize.height ); on viewDidLayoutSubviews you'll see that the size has changed.

It happens on the axis which the offset value is not zero. So, to solve it, you should set it to zero CGPointZero before it appears.

The @Steph Sharp answer solve my problem except if I open a viewcontroller as a popup (presentViewController). I tried to compensate the value by adding the bug amount on viewDidLayoutSubviews but it bugs the paging.

Short answer, I end up with:

- (void) viewDidLayoutSubviews
{
    [super viewDidLayoutSubviews];
    scrollView.contentOffset = previousPoint;
}

- (void) viewWillAppear:(BOOL)animated
{
    scrollView.contentOffset = CGPointZero;
}

- (void) viewWillDisappear:(BOOL)animated
{
    previousPoint = scrollView.contentOffset;
    scrollView.contentOffset = CGPointZero;
}

Now it works when I leave the section or if I open a popup above.

Solution 5

Check if the contentSize property of your scrollview is CGSizeZero. I had a similar problem because the contentSize had been not set.

Share:
19,176
Sergey Grischyov
Author by

Sergey Grischyov

St. Petersburg State University graduate. Cocoa Controls contributor.

Updated on July 27, 2022

Comments

  • Sergey Grischyov
    Sergey Grischyov almost 2 years

    I have a UIViewController in Interface Builder, and I add a UIScrollView to the UIView. It has a contentOffset property equal to 0.0, but when I scroll the UIScrollView to the very bottom and push another view the contentOffset changes.

    NSLog gives me the following values:

    //View just loaded
    2013-09-09 16:19:27.455 my_app[24588:907] Scroll View content offset is 0.000000
    
    //We came back from another view
    2013-09-09 16:19:30.957 my_app[24588:907] Scroll View content offset is 108.000000
    

    What's the reason of this behaviour? I've just added a UIScrollView in IB and did nothing in code at all.

    The contentHeight of the scrollView is bigger than self.view.height. The frame of the scrollView stays the same.

  • Sergey Grischyov
    Sergey Grischyov over 10 years
    Oh gosh, what a horrible bug! Hope Xcode 5 and iOS 7 have it fixed. Thanks for the ultimate answer!
  • Steph Sharp
    Steph Sharp over 10 years
    No problems, I'm hoping it gets fixed soon too :)
  • jc_35
    jc_35 about 10 years
    Be careful, this is just for iOS7 min
  • William Robinson
    William Robinson over 9 years
    This was the best fix for me, setting the contentOffset to 0 caused a visible jump.
  • Petar
    Petar almost 9 years
    Thiis does not fix it for me a and I have to follow the guidelines in the accepted answer and manually update the contentOffset in viewDidLayoutSubviews . Any idea what is going on with that problem - its 2015 !
  • Pavan
    Pavan almost 7 years
    hah, StephSharp @SergeyGrischyov 5 years on and this problem still persists.
  • code4j
    code4j about 6 years
    Only this works, I even tried UIScrollViewContentInsetAdjustmentNever which also failed.