I think all day to change the navigation Bar title color, but it doesn\'t work. this is my code:
var user: User? {
didSet {
navigationItem.title = us
The title color of Navigation Bar can be changed in Storyboard.
Go to Attributes inspector of Navigation Controller > Navigation Bar and set the desired color in Title Color menu.
Add this in your code . .
let textAttributes = [NSForegroundColorAttributeName:UIColor.red]
navigationController?.navigationBar.titleTextAttributes = textAttributes
SWIFT 4:
let textAttributes = [NSAttributedStringKey.foregroundColor:UIColor.red]
navigationController?.navigationBar.titleTextAttributes = textAttributes
SWIFT 4.2+:
let textAttributes = [NSAttributedString.Key.foregroundColor:UIColor.red]
navigationController?.navigationBar.titleTextAttributes = textAttributes
Keeping all the other attributes of the title: If you just want change the color you could do like this:
if var textAttributes = navigationController?.navigationBar.titleTextAttributes {
textAttributes[NSAttributedString.Key.foregroundColor] = UIColor.red
navigationController?.navigationBar.titleTextAttributes = textAttributes
}
If you set your navigation bar's title to prefer large titles, like so:
navigationBar.prefersLargeTitles = true
then you need to use the largeTitleTextAttributes property and not the titleTextAttributes property. If you set your nav title to be a large title, the titleTextAttribute is not the correct property to use. Use the largeTitleTextAttributes property, like so:
navigationBar.largeTitleTextAttributes = [.foregroundColor: UIColor.white]
Swift 4
set this first
navigationController?.navigationBar.barStyle = .default
then one of those should work
navigationBar.largeTitleTextAttributes = [NSAttributedStringKey.foregroundColor: UIColor.red]
or
navigationBar.titleTextAttributes = [NSAttributedStringKey.foregroundColor: UIColor.red]
Swift 5
navigationBar.largeTitleTextAttributes = [NSAttributedString.Key.foregroundColor: UIColor.red]
or
navigationBar.titleTextAttributes = [NSAttributedString.Key.foregroundColor: UIColor.red]
you can just add this line of code in your AppDelegate in the func
didFinishLaunchingWithOptions
Code you will add to change title color:
UINavigationBar.appearance().titleTextAttributes = [NSAttributedString.Key.foregroundColor : UIColor(red: 0.7415059209, green: 0.5448099971, blue: 0.5051562786, alpha: 1)]
you can add this line as well if you wanna change the backButton color
UINavigationBar.appearance().tintColor = #colorLiteral(red: 0.7415059209, green: 0.5448099971, blue: 0.5051562786, alpha: 1)
Finally you can add this line to change the background color:
UINavigationBar.appearance().barTintColor = #colorLiteral(red: 0.2000651062, green: 0.1960035861, blue: 0.2000851929, alpha: 1)
Note: You can change the values dependent on the color you want and have a happy coding day