简体   繁体   中英

How to change UINavigationBar font & color?

I want to change font and color of my UINavigationBar from the AppDelegate. For this I do:

let appearance = UINavigationBar.appearance()
    appearance.translucent = false
    appearance.titleTextAttributes = [NSFontAttributeName: UIFont(name: "Chalkduster", size: 21)!]
    appearance.barTintColor = UIColor(red: 80/255, green: 185/255, blue: 225/255, alpha: 1)
    appearance.titleTextAttributes = [NSForegroundColorAttributeName: UIColor.whiteColor()]

but in this case works just one of them, for example:

if I set at first - Font, at the second - color, it just change color, not font, too. If I set at first - Color, at the second - Font, it just change font, not color, too.

How can I change both of them?

let appearance = UINavigationBar.appearance()
    appearance.translucent = false
    appearance.barTintColor = UIColor(red: 80/255, green: 185/255, blue: 225/255, alpha: 1)
    appearance.titleTextAttributes = [NSFontAttributeName: UIFont(name: "Chalkduster", size: 21)!, NSForegroundColorAttributeName: UIColor.whiteColor()]

Think about it. You are saying:

appearance.titleTextAttributes = [NSFontAttributeName: UIFont(name: "Chalkduster", size: 21)!]
appearance.titleTextAttributes = [NSForegroundColorAttributeName: UIColor.whiteColor()]

So in the second line you throw away the title text attributes you set in the first line. This is not additive in some magical way: you are replacing what you did in the first line with what you do in the second line.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM