I am trying to set the title color in my UINavigationBar in my AppDelegate.swift, like this-
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
// Override point for customization after application launch.
UINavigationBar.appearance().barTintColor = UIColor(red: 26.0/255.0, green: 188.0/255.0, blue: 156.0/255.0, alpha: 1.0)
UINavigationBar.appearance().tintColor = UIColor(red: 255.0/255.0, green: 255.0/255.0, blue: 255.0/255.0, alpha: 1.0)
UINavigationBar.appearance().titleTextAttributes = [NSForegroundColorAttributeName: UIColor.white]
UINavigationBar.appearance().titleTextAttributes = [NSFontAttributeName: UIFont(name: "Pacifico", size: 24)!]
// Turquoise color rgba(26, 188, 156,1.0)
return true
}
But it doesn't work. The result looks like
Why isn't this working? Thanks!
You're overwriting the titleTextAttributes value you set initially with the color to a new value that includes only the font.
You should combine your attributes and then set them at once:
Edit: Swift 4
let color = UIColor.white
let font = UIFont(name: "Pacifico", size: 24)!
let attributes: [NSAttributedStringKey: AnyObject] = [
NSAttributedStringKey.font: font,
NSAttributedStringKey.foregroundColor: color
]
UINavigationBar.appearance().titleTextAttributes = attributes
Swift 3
let color = UIColor.white
let font = UIFont(name: "Pacifico", size: 24)!
let attributes: [String: AnyObject] = [
NSFontAttributeName: font,
NSForegroundColorAttributeName: color
]
UINavigationBar.appearance().titleTextAttributes = attributes
Swift 4:
For NavigationBar Background color:
UINavigationBar.appearance().barTintColor = UIColor(red: 50/255, green: 90/255, blue: 150/255, alpha: 1)
For NavigationBar title color and font:
let attrs = [
NSAttributedStringKey.foregroundColor: UIColor.red,
NSAttributedStringKey.font: UIFont(name: "Georgia-Bold", size: 24)!
]
UINavigationBar.appearance().titleTextAttributes = attrs
reference: here
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