roro roro - 20 days ago 5
iOS Question

UINavigationController bar covers its uiviewcontroller's content

I have a UIViewController, MyViewController, with a UIToolbar at the top. Using interface builder the doc outline looks like this:

View
- subview1
- subview2
- UIToolbar
- Bar Button Item1
- Bar Button Item2
- Bar Button Item3


MyViewController is used throughout my app. Sometimes it's in a UINavigationcontroller, other times it's in a UIPopoverView.

When it appears in UINavigationController, the navigation bar covers up the UIToolbar and all other content near the top of View. In this screenshot you can see that the UIToolbar is completely covered up, and UIButton w/ an image of a green light bulb is partially covered.

According to the apple documentation


Any view that needs to be anchored to the top and just below the status bar (i.e. UIToolbar, UIButton, etc.) requires additional work for proper placement.


It proceeds to give a solution that simply uses constraints to move your VC's content down by x pixels, in my case the UIToolbar. This doesn't seem like a good solution because it assumes you always want the content moved down below the nav bar. In my case, I obviously don't since MyViewController is not always in a UINavigationController. When I use the constraint solution provided in these docs, the UIToolbar is oddly floating down x pixels unanchored from the top in all cases where MyViewController is not in a UINavigationController.

Am I missing something with how I am supposed to display a VC's content within a UINavigationController? Thanks so much.

Answer

In iOS7, UINavigationBar has a translucent property. By this, the view of the child VC of UINavigationViewController is under UINavigationBar by default.

If you don't need to get an translucent effect, turn off this property using the following code.

self.navigationController.navigationBar.translucent = NO

You can also use @ldindu's way to achieve your need.

Comments