2015-10-13 13 views
0

我的NavigationBar上有幾個導航項(後退按鈕,保存按鈕)。 我試着尋找相關的答案,我發現一個過時的答案或只是沒有。如何更改導航項目的字體?

我試過了navigationItem.leftBarButtonItem的setTitleTextAttributes,但沒有工作。

我設法改變NavigationBar標題的字體,但就是這樣。使用self.navigationBar.titleTextAttribues,使用與導航項目相同的方法但不工作。

編輯:找到了解決辦法,但我必須添加此每視圖控制器viewWillAppear中

let barBtn = UIBarButtonItem(title: "Test", style: UIBarButtonItemStyle.Bordered, target: self, action: "backButtonPressed") 
barBtn.setTitleTextAttributes([NSFontAttributeName: UIFont(name: FONT_HERE, size: 12)!], forState: UIControlState.Normal) 
self.navigationItem.setHidesBackButton(true, animated: false) 
self.navigationItem.setLeftBarButtonItem(barBtn, animated: false) 

然後加入backButtonPressed功能,我叫popViewControllerAnimated。

你覺得呢,夥計們?有一個更簡單的解決方案嗎?

+4

的可能重複[我怎樣才能改變後退按鈕的字體爲我的導航欄?](http://stackoverflow.com/questions/27583346/how-can-i-change-the-font後退按鈕爲我的導航欄) –

+0

跟着它,不工作。 –

+0

你可以指定一個自定義標籤self.navigationItem.titleView – engmahsa

回答

0

我建議你使用外觀協議:https://developer.apple.com/library/prerelease/ios/documentation/UIKit/Reference/UIAppearance_Protocol/index.html

下面是我如何自定義導航欄的外觀的例子。

//導航欄標題 [[UINavigationBar的外觀] setTitleTextAttributes:@ {NSFontAttributeName:[UIFont fontWithName:@ 「myfont」 尺寸:20.0f], NSForegroundColorAttributeName:的UIColor whiteColor] }];

// Navigation bar buttons and background 
[UINavigationBar appearance].tintColor = [UIColor whiteColor]; 
[UINavigationBar appearance].barTintColor = [UIColor teal]; 
[UINavigationBar appearance].translucent = NO; 
+2

UINavigationBar沒有成員'setTitleTextAttributes' –

+0

不,它的外觀有!請檢查代碼。 –