0

In my iOS application I am using a UINavigationBar without a UINavigationController. How can I still set the color of the application statusbar to match the color of my UINavigationBar, which is a custom blue color? Tried but doesn't work:

  • Setting the UIViewControllerBasedStatusBarAppearance property in the plist doesn't have any effect
  • Setting the barTintColor of my UINavigationBar does not affect the statusbar, presumably because it's not managed by a UINavigationViewController?
  • Setting UINavigationBar.appearance().barTintColor doesn't work either

Any suggestions?

FalFire
  • 11
  • 5

2 Answers2

1

Ok I just figured it out thanks to the accepted answer in this question: How to add custom navigation bar to a full-screen VC in iOS 7 and make it tint the status bar to match?

Adopt the UINavigationBarDelegate protocol in your viewcontroller and implement the following method (Swift):

func positionForBar(bar: UIBarPositioning) -> UIBarPosition {
    return UIBarPosition.TopAttached
}

Apparently, all navigation bars positioned in this way extend their barTintColor to the statusbar.

Community
  • 1
  • 1
FalFire
  • 11
  • 5
  • This worked for me - just remember that you need to make sure your view controller is set as the delegate for the `UINavigationBar` either explicitly in code with: `myNavigationBar.delegate = self` in `viewDidLoad` OR use interface builder and connect the delegate outlet of the navigation bar to your view controller – Nick Ager Jan 21 '16 at 13:38
-2

Add this method to your view controller:

- (UIStatusBarStyle)preferredStatusBarStyle
{
    return UIStatusBarStyleLightContent; // or UIStatusBarStyleDefault
}
rounak
  • 9,217
  • 3
  • 42
  • 59
  • Thanks for replying but please read my question, I want to match the *color* of my UINavigationBar, which is a custom blue color in this case. – FalFire Jun 28 '15 at 19:05
  • The status bar comes in just two variants. What exactly do you mean by matching? – rounak Jun 28 '15 at 19:13
  • Well if you set the barTintColor of a UINavigationBar when it's managed by a UINavigationController, the statusbar gets the same color. My problem is: this does not happen when you use a UINavigationBar on its own... – FalFire Jun 28 '15 at 19:20