I have a problem when dragging a navigation bar or toolbar (storyboard) to my view controller.
UINavigationBar:
As you can see in the image above, the right button is almost overlapping the status bar.
With a UIToolbar it happens the same:
This view controllers are intended to be used as a Modal, that's the reason I'm not using a UINavigationController.
In another section I use a UINavigationController and it appears as I expect:
How can I drag a UINavigationBar / UIToolbar to a view controller without overlapping the status bar?
The navigation bars or toolbars have to be at (0,
viewController.topLayoutGuide.length
) with bar positioning ofUIBarPositionTopAttached
. You should set the delegate of your navigation bar or your toolbar to your view controller, and returnUIBarPositionTopAttached
. If positioned correctly, you will have the result in your third image.More information here: https://developer.apple.com/documentation/uikit/uibarpositioningdelegate?language=objc
You can resolve this issue by using Auto Layout, as per this techincal note (Preventing the Status Bar from Covering Your Views).
Here are some excerpts:
This will support ios6 and ios7.
I gave up and had to set the navbar height constraint to 64 in x xib based VC cause viewController.topLayoutGuide.length is 0 in viewDidLoad despite statusbar being present :-[ which means in a non universal app on ipad you'd have 20 px on the top of the view wasted (cause status bar is separate from the iphone simulation window)
Do these steps
Drag the NavigationBar to your ViewController in Xib, set the ViewController as its delegate. Note that the NavigationBar should be at (0, 20)
In ViewController, conform to the
UINavigationBarDelegate
Implement this method
positionForBar tells the NavigationBar if it should extend its background upward the Status Bar
Please see my answer here, I've copied the content below for convenience:
https://stackoverflow.com/a/18912291/1162959
The easiest workaround I've found is to wrap the view controller you want to present inside a navigation controller, and then present that navigation controller.
Advantages:
Disadvantages:
You can also manage it by increasing height of navigation bar by providing image of size 620x128 for ios version. And this image is used in :