2015-09-22 54 views
0

我想從AppDelegate更改UINavigationBar的字體和顏色。爲此,我做的事:如何更改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()] 

但在這種情況下,工作只是其中之一,例如:

如果我首先設置 - 字體,在第二次 - 顏色,它只是改變顏色,而不是字體也是。如果我首先設置 - 顏色,在第二個 - 字體,它只是改變字體,而不是顏色。

我該如何改變它們?

+0

「我想改變字體和我的UINavigationController的顏色」一個UINavigationController不是一個視圖。它沒有字體或顏色。 – matt

回答

3
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()] 
+0

謝謝!有用! –

0

想一想。你說:

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

所以在第二行,你扔掉標題文本屬性您在第一行中。這不是以某種神奇的方式添加的:你用代替你在第一行中做了什麼,以及你在第二行做了什麼。