2

I use Xcode 5 to create a master detail project. The default navigation bar looks like this:

enter image description here

Now I create a new UIViewContrller file and its xib file:

enter image description here

I add a NavigationBar to the view, and set background color green, but the status bar area is in navigation bar.How to config it as MasterController navigation bar display?

enter image description here

Naga Mallesh Maddali
  • 1,005
  • 10
  • 19
qichunren
  • 4,405
  • 7
  • 37
  • 48
  • Try answer present in this post : http://stackoverflow.com/questions/24484352/navigation-bar-and-status-bar-issue-in-ios-7/24484747#24484747 – Naga Mallesh Maddali Jul 17 '14 at 15:52
  • @NagaMalleshMaddali I add `self.edgesForExtendedLayout = UIRectEdgeNone;` to PopViewController viewDidLoad method, and no changes in simulator. – qichunren Jul 17 '14 at 15:55
  • Try adding this in viewDidLoad method : self.navigationController.navigationBar.translucent = NO; – Naga Mallesh Maddali Jul 17 '14 at 15:56
  • @NagaMalleshMaddali Not worked as your tips. I then make a IBoutlet for Navigation bar, and add `_navigationBar.translucent = NO;` in viewDidLoad method, and found Navigation bar background color is not green, is default color. – qichunren Jul 17 '14 at 16:05
  • How to make Navigation bar height looks as it plus status bar height? – qichunren Jul 17 '14 at 16:07
  • This is a normal view controller I create, I then drag a NavigationBar to the controller's view top area in Interface. – qichunren Jul 17 '14 at 16:09
  • Above suggestions works if you use a UINavigationController. You are seeing problem because you have used a UINavigationBar directly. Try solutions in this post : http://stackoverflow.com/questions/18901753/ios-7-navigation-bar-toolbar-buttons-very-close-to-status-bar – Naga Mallesh Maddali Jul 17 '14 at 16:16
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/57517/discussion-between-naga-mallesh-maddali-and-qichunren). – Naga Mallesh Maddali Jul 17 '14 at 16:33
  • I drag navigation bar 20px, then use navigation bar delegate, it works. Thanks. – qichunren Jul 17 '14 at 16:36

1 Answers1

5

You have 2 choices.

  1. Either create a UINavigationViewController, and set your ViewController has the rootViewController of that UINavigationViewController (it will automatically create a navigation bar with the proper status bar color on top of it),

  2. OR you can keep your navigationBar, set its Y origin to be 20, and do the following:

In the .h file:

@interface XYZViewController : UIViewController <UIBarPositioningDelegate>

And in the .m file:

- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationBar.delegate = self;
}

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

EDIT: How to make the bar resize in phone landscape mode:

- (void)willAnimateRotationToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration {
    [self.navigationBar sizeToFit];
}
Jeffrey Sun
  • 7,789
  • 1
  • 24
  • 17