Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

iOS 7 | Navigation bar / Toolbar buttons very close to status bar

I have a problem when dragging a navigation bar or toolbar (storyboard) to my view controller.

UINavigationBar:

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:

UIToolbar

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:

UINavigationController

How can I drag a UINavigationBar / UIToolbar to a view controller without overlapping the status bar?

like image 608
Axort Avatar asked Sep 19 '13 17:09

Axort


3 Answers

The navigation bars or toolbars have to be at (0, viewController.topLayoutGuide.length) with bar positioning of UIBarPositionTopAttached. You should set the delegate of your navigation bar or your toolbar to your view controller, and return UIBarPositionTopAttached. If positioned correctly, you will have the result in your third image.

More information here: https://developer.apple.com/documentation/uikit/uibarpositioningdelegate?language=objc

like image 64
Léo Natan Avatar answered Nov 11 '22 03:11

Léo Natan


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

@interface ETPViewController () <UINavigationBarDelegate>

Implement this method

- (UIBarPosition)positionForBar:(id <UIBarPositioning>)bar
{
    return UIBarPositionTopAttached;
}

positionForBar tells the NavigationBar if it should extend its background upward the Status Bar

like image 25
onmyway133 Avatar answered Nov 11 '22 01:11

onmyway133


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.

MyViewController *vc = [MyViewController new];
UINavigationController *nav = [[UINavigationController alloc] 
    initWithRootViewController:vc];
[self presentViewController:nav animated:YES completion:NULL];

Advantages:

  • No mucking with frames needed.
  • Same code works on iOS 6 an iOS 7.
  • Less ugly than the other workarounds.

Disadvantages:

  • You'll probably want to leave your XIB empty of navigation bars or toolbars, and programatically add UIBarButtonItems to the navigation bar. Fortunately this is pretty easy.
like image 10
bobics Avatar answered Nov 11 '22 01:11

bobics