如何更改 UINavigationBar 的字体和颜色?

How to change UINavigationBar font & color?

我想从 AppDelegate 更改 UINavigationBar 的字体和颜色。为此,我这样做:

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()]

但在这种情况下只适用其中之一,例如:

如果我首先设置 - 字体,在第二个 - 颜色,它只是改变颜色,而不是字体。如果我首先设置 - 颜色,在第二个 - 字体,它只会改变字体,而不是颜色。

如何更改它们?

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()]

想一想。你是说:

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

所以在第二行中,您丢弃您在第一行中设置的标题文本属性。这不是以某种神奇的方式添加:您是 用第二行中的内容替换第一行中的内容。