I am trying to fill the status bar background color to orange using the following
UINavigationBar.appearance().tintColor = UIColor.orangeColor()
UINavigation
This is how I did it without adding a view in a VC with in a NavBarController
I wanted the color of the status bar to be the same as the VC view color so I just wrote:
override func viewDidLoad() {
super.viewDidLoad()
self.view.backgroundColor = UIColor.grayColor()
self.navigationController?.navigationBar.clipsToBounds = true
}
Try it.
Simon's answer in swift 3
let view = UIView(frame: CGRect(x: 0.0, y: 0.0, width: UIScreen.main.bounds.size.width, height: 20.0))
view.backgroundColor = .orange
self.view.addSubview(view)
There is one other way I know which uses private api. This has some benefits when orientation changes and keyboard is presented and view move up. I've used it and was lucky every time (app was released in the app store).
func setStatusBarBackgroundColor(color: UIColor) {
guard let statusBar = UIApplication.shared.value(forKeyPath: "statusBarWindow.statusBar") as? UIView else { return }
statusBar.backgroundColor = color
}
UINavigationBar.appereance()
works for upcoming viewControllers, but not the currently displayed rootViewController. To achieve this I have added the following to my didFinishLaunchingWithOptions
:
UINavigationBar.appearance().tintColor = myColor
let navigationController = UIApplication.sharedApplication().windows[0].rootViewController as! UINavigationController
navigationController.navigationBar.barTintColor = myColor
navigationController.navigationBar.titleTextAttributes = [NSForegroundColorAttributeName : myTextColor]
navigationController.navigationBar.translucent = false
navigationController.setNeedsStatusBarAppearanceUpdate()