IOS7 : UIScrollView offset in UINavigationController

UinavigationcontrollerUinavigationbarIos7

Uinavigationcontroller Problem Overview


I'm currently migrating my app on ios 7 and I've been stuck for hours on the new navigationcontroller/bar management.

Before, when we had a navigation controller, we had a snippet like this :

UINavigationController *navController = [[UINavigationController alloc]initWithRootViewController:[[MainViewController alloc]init]];

In interface builder, we had the choice to set an existing navigationbar for the view and everything match the content of the real view.

OK so now, i have no clue of how to design properly with interface builder. I still have my snippet to initialize my navcontroller. However in the interface builder for my MainViewController if I set a status bar to translucent or opaque navigation bar, i have an offset of 44px at the top (see below).


Interface Builder_________________________And the result

https://i.stack.imgur.com/JODqs.png" width="300" height="500" /> https://i.stack.imgur.com/AJjtd.png" width="300" height="525" />


Now, if i set status bar to none, there is no offset at top but since the view on simulator is smaller because of navigation bar the bottom of the view in interface builder is cut off.

Interface Builder_________________________And the result

https://i.stack.imgur.com/RIhjR.png" width="300" height="500" /> https://i.stack.imgur.com/PC96b.png" width="300" height="540" />

I guess i'm really missing something here but i can't find any topic or apple info in iOS7 Transitions Guide about that.

Thanks for your help


EDIT

As we can see in the pictures, the first child of the view is a UIScrollView which contains both labels, the problem does not appear when there is no scrollview. It also appears if it's a UITableView. If a label is outside the UIScrollView, there is no offset to that label.

Uinavigationcontroller Solutions


Solution 1 - Uinavigationcontroller

OK so i found the solution, I have set in my controller the property:

self.automaticallyAdjustsScrollViewInsets = false

I don't really understand the true benefit of this property though, (or why the default value is true)

The only documentation i found was there:

Update

In iOS 11 automaticallyAdjustsScrollViewInsets is deprecated

You should now use:

self.tableView.contentInsetAdjustmentBehavior = .never

I also encourage you to check this question and its answer to get a better understanding of those properties

Solution 2 - Uinavigationcontroller

@Justafinger's answer worked like a charm for me as well.

Just wanted to add that this setting can also be adjusted easily from the interface builder.

  1. Select your view controller
  2. Click the 'Attributes Inspector' tab
  3. Uncheck 'Adjust Scroll View Insets'
  4. Enjoy!

enter image description here

Solution 3 - Uinavigationcontroller

I was running into this same issue, but I found a rather odd property on the ViewController in interface builder that seems to have been causing this for me. There is an "Extend Edges" set of check boxes. I removed the "Under Top Bars" check, and everything started laying out properly for me.

Solution 4 - Uinavigationcontroller

With automaticallyAdjustsScrollViewInsets set to YES (the default setting) there is a mismatch in scrollview positioning between ios6 and ios7, so to make them consistent you need to disable this setting. However, ios6 will crash if it comes across automaticallyAdjustsScrollViewInsets, so you either need to make a programatic change of automaticallyAdjustsScrollViewInsets conditional on ios7 or else switch off the option using the storyboard/NIB

Solution 5 - Uinavigationcontroller

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 6 - Uinavigationcontroller

I also face this problem.

UIScrollView content size is calculate by OS as other sizes, origins provided by constraint system - that's why OS has doubtfulness.

How to fix - You should explicitly define content size of UIScrollView:

  1. Embed scrollable content to UIView (I rename it to ContentView)
  2. Add constraints:

ContentView.Weight = View.Weight and ContentView.Height = View.Height

enter image description here

Solution 7 - Uinavigationcontroller

It seems like a work around solution is to view the storyboard file as "iOS 6.1 and earlier" (select storyboard file->File inspector->Interface Builder Document->View As. Positioning subviews in this mode shows the offset.

Solution 8 - Uinavigationcontroller

Thank you guys for the solutions! I struggled for hours trying to solve the problem. Everything was ok when there was no Navigation Bar involved but it went haywire the moment I embedded the ViewController in a NavigationController.

I solved it by unchecking the Adjust Scroll View Insets and the Under Top Bars. Both of these are located in the ViewController's Attribute Inspector. Thanks a million!

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionstreemView Question on Stackoverflow
Solution 1 - UinavigationcontrollerstreemView Answer on Stackoverflow
Solution 2 - UinavigationcontrollerMyxticView Answer on Stackoverflow
Solution 3 - UinavigationcontrollerBen NicholasView Answer on Stackoverflow
Solution 4 - UinavigationcontrollerdawidView Answer on Stackoverflow
Solution 5 - UinavigationcontrollerChuy47View Answer on Stackoverflow
Solution 6 - UinavigationcontrollermaslovsaView Answer on Stackoverflow
Solution 7 - UinavigationcontrollerswhitmanView Answer on Stackoverflow
Solution 8 - UinavigationcontrollerJackson GanView Answer on Stackoverflow