1

I´m making an app with Swift and I want the status bar and the navigation bar to have different colors, like the google translate app or the google music app on iOS.

I already know that the colors of the status bar and the navigation bar can´t be set separately, but is there any type of trick to do it?

Daplerio
  • 155
  • 2
  • 9
  • 1
    Welcome to Stack Overflow! This question is a little short on information. Can you share what you have tried, and what problems have you run into? – Jay Blanchard Mar 24 '15 at 17:25
  • @JayBlanchard The problem is that I want the status bar and the navigation bar to have different colores but in iOS by default they share the same color. But like I said before some apps have them with different colors. – Daplerio Mar 24 '15 at 18:11

3 Answers3

6

A simple way to do this would be to add a view on top of the navigation bar that is the exact size of the status bar.

For instance, to make the status bar become red, you could add the following to your viewDidLoad method:

let statusBarHeight = CGFloat(20)
let colorView = UIView(frame: CGRectMake(0, -statusBarHeight, self.view.bounds.width, statusBarHeight))
colorView.backgroundColor = UIColor.redColor()
self.navigationController?.navigationBar.addSubview(colorView)
derdida
  • 14,784
  • 16
  • 90
  • 139
juliensaad
  • 2,019
  • 2
  • 20
  • 27
  • Just wanted to clarify, has anyone done this and got the app on App Store? I wanted to use this in my current app, but worried about Apple's guidelines. – Shubham Naik May 08 '17 at 11:34
  • Yes, nothing about this is against Apple's guidelines. You are simply adding a view in your navigation bar, which is perfectly fine. – juliensaad May 08 '17 at 18:06
1

https://gist.github.com/wh1pch81n/ef921944f732de1c45bb93646b462d6f

You can get a reference to the status bar. Then change the background color property.

UIView *subViewWindow = [[UIApplication sharedApplication] valueForKey:@"statusBarWindow"];
((UIView *)subViewWindow[0]).backgroundColor = UIColor.redColor;
DerrickHo328
  • 4,664
  • 7
  • 29
  • 50
0

UINavigationBar's setBackgroundImage:forBarMetrics: is a nice solution for this case. I've used this method in my obj-C Application and it has solved a similar problem pretty well. For the plain background colour I have made a one-pixel-width monochrome image.

takatan
  • 51
  • 5