qichunren
qichunren

Reputation: 4475

How to make NavigationBar and StatusBar same background in iOS 7?

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

Upvotes: 2

Views: 1853

Answers (1)

Jeffrey Sun
Jeffrey Sun

Reputation: 8049

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];
}

Upvotes: 5

Related Questions