NavigationBar bar, tint, and title text color in iOS 8
Solution 1
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
Solution 2
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
}
Solution 3
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.
Solution 4
Swift 5
self.navigationController?.navigationBar.titleTextAttributes = [NSAttributedString.Key.foregroundColor: UIColor.white]
Swift 4
self.navigationController?.navigationBar.titleTextAttributes = [NSAttributedStringKey.foregroundColor: UIColor.white]
Solution 5
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
Related videos on Youtube
Comments
-
AG1 almost 2 years
The background text in the status bar is still black. How do I change the color to white?
// io8, swift, Xcode 6.0.1 override func viewDidLoad() { super.viewDidLoad() self.navigationController?.navigationBar.barTintColor = UIColor.blackColor() self.navigationController?.navigationBar.titleTextAttributes = [NSForegroundColorAttributeName: UIColor.orangeColor()] }
-
AG1 over 9 yearsHow do I override the NavigationController to use these methods?
-
Alex over 9 yearsYou don't override the NavigationController, you just modify it in the ViewDidLoad function. I updated my answer above.
-
Saorikido over 9 yearsBut I'm using iso 8.1
-
AG1 over 9 yearsSo a developer just has to add NavigationController.swift to the workspace? If so that is neat!
-
kakubei over 9 years@AG1 This is incorrect, just adding a new file NavigationController.swift is not enough, you have to hook up your Navigation Controller in the Storyboard to this file in the Identity Inspector since by default it will use a Generic UINavigationController.
-
Adam Johns almost 9 yearswhy viewWillAppear()? I am doing this in viewDidLoad and it works fine.
-
user3163404 over 8 yearsBut when I go back to another viewcontroller the color has changed also. Any idea on how to prevent this. I want mine main viewcontroller to be one color and the others another.
-
Niko Klausnitzer over 8 yearsI think you also have to set the color in your new view controller.
-
FractalDoctor over 8 yearsBecause every time you view the UIView you'd like it to be set. If you set in viewDidLoad() say in a UITabBar view, move to another tab where it's set again, then come back it'll be overwritten as the original view was only loaded once.
-
sumit about 8 yearswhat if we have mutiple navigation bars ?
-
triiiiista almost 8 yearstintColor doesn't work for Xcode 7.3.1. The text is still black.
-
Markymark over 7 yearsYou can also just do all of this in the storyboard w/out needing to create a custom navigation controller. The properties for Bar Tine and Style are right there on the right under the Attributes inspector.
-
oky_sabeni over 7 yearsIn Xcode 8.2, the titleTextAttributes does not autocomplete but it works.
-
Chauyan over 7 yearsYes, either you put your setting value in UINavigationController ViewDidLoad or you put these setting value in UIViewController ViewWillApear function, both work for me
-
Ahmadreza over 6 yearsI get: Type 'NSAttributedStringKey' (aka 'NSString') has no member 'foregroundColor'
-
Alan Scarpa about 6 yearsUpdate for swift 4 -
NSForegroundColorAttributeName
should now beNSAttributedStringKey.foregroundColor
-
Sylvan D Ash about 6 yearsNOTE: this will change the title color for all controllers, even the parent. If you want the parent to still retain it's color, you can do so in the
willMove(toParentViewController parent: UIViewController?)
function of the child -
31piy almost 6 yearsWhile this code snippet may solve the question, including an explanation really helps to improve the quality of your post. Remember that you are answering the question for readers in the future, and those people might not know the reasons for your code suggestion.
-
rgkobashi over 5 yearsis barTintColor still necessary? deleting that line still works
-
Bhavsang Jam over 5 yearsPlease use below code for latest version xcode 10 and swift 4.2 self.navigationController?.navigationBar.titleTextAttributes = [NSAttributedString.Key.foregroundColor: UIColor.white]
-
Stephane Paquet over 5 yearsUpdate for Swift 4.2:
NSAttributedString.Key.foregroundColor
instead ofNSForegroundColorAttributeName
-
Ben about 5 yearsI really liked how you added this answer. .barTintColor isn't an option now. Do you mean .backgroundColor?
-
Ram Madhavan over 3 yearsThis is adding a text attribute. Not actually changing the color of the title. If you note the font of the text you can understand the difference.
-
Ram Madhavan over 3 yearsThis is adding a text attribute. Not actually changing the color of the title. If you note the font of the text you can understand the difference.
-
Muhammad Danish Qureshi over 2 years@Albert Vila Calvo in iOS 15 this thing is not working, can you tell me how to do that?