Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

UIScrollView contentOffset change after another view pushed

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.

like image 856
Sergey Grischyov Avatar asked Sep 09 '13 12:09

Sergey Grischyov


3 Answers

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

like image 118
Alex Avatar answered Sep 22 '22 13:09

Alex


See my answer to a similar question.

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

Also, see this:

  • This answer to the question UIScrollView's origin changes after popping back to the UIViewController.
  • This related question, UIScrollview Autolayout Issue.
like image 21
Steph Sharp Avatar answered Sep 21 '22 13:09

Steph Sharp


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!

like image 32
Chuy47 Avatar answered Sep 24 '22 13:09

Chuy47