One of the issues that can occur is coloring the nav bar when your keyboard is open for typing. To fix this, head into General Settings –> Color of Navigation Bar when Keyboard is opened. Set this color to black as well. This should fix any issues you may have when the keyboard is open.
In AppDelegate.swift
, in application(_:didFinishLaunchingWithOptions:)
I put the following:
UINavigationBar.appearance().barTintColor = UIColor(red: 234.0/255.0, green: 46.0/255.0, blue: 73.0/255.0, alpha: 1.0)
UINavigationBar.appearance().tintColor = UIColor.white
UINavigationBar.appearance().titleTextAttributes = [NSAttributedString.Key.foregroundColor : UIColor.white]
(For Swift 4 or earlier use NSAttributedStringKey
instead of NSAttributedString.Key
)
For titleTextAttributes
, the docs say:
You can specify the font, text color, text shadow color, and text shadow offset for the title in the text attributes dictionary
I like Alex's answer. If you want something quick to try out in a ViewController
make sure you use
viewWillAppear()
override func viewWillAppear(animated: Bool) {
super.viewWillAppear(animated)
var nav = self.navigationController?.navigationBar
nav?.barStyle = UIBarStyle.Black
nav?.tintColor = UIColor.white
nav?.titleTextAttributes = [NSForegroundColorAttributeName: UIColor.orange]
//nav?.titleTextAttributes = [NSAttributedString.Key.foregroundColor: UIColor.orange] // swift 4.2
}
To change the color universally, this code should sit in the NavigationController
's viewDidLoad
function:
class NavigationController: UINavigationController, UIViewControllerTransitioningDelegate {
override func viewDidLoad() {
super.viewDidLoad()
// Status bar white font
self.navigationBar.barStyle = UIBarStyle.Black
self.navigationBar.tintColor = UIColor.whiteColor()
}
}
To change it per ViewController
you would have to reference the NavigationController
from the ViewController
and write similar lines in that ViewController
's viewWillAppear
function.
Swift 5
self.navigationController?.navigationBar.titleTextAttributes = [NSAttributedString.Key.foregroundColor: UIColor.white]
Swift 4
self.navigationController?.navigationBar.titleTextAttributes = [NSAttributedStringKey.foregroundColor: UIColor.white]
To work in objective-c I have to put the following lines in viewWillAppear
in my CustomViewController.
[self.navigationController.navigationBar setBarTintColor:[UIColor whiteColor]];
[self.navigationController.navigationBar setTranslucent:NO];
For Swift2.x this works:
self.navigationController?.navigationBar.barTintColor = UIColor.redColor()
For Swift3.x this works:
self.navigationController?.navigationBar.barTintColor = UIColor.red
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With