2013-10-01 61 views
0

我有這個在我的UINavigationController的顏色:更改UINavigationController的按鈕

(void)viewDidLoad 
{ 
    [super viewDidLoad]; 
// Do any additional setup after loading the view. 

    self.navigationBar.tintColor = [UIColor whiteColor]; 
    self.navigationBar.barTintColor = [UIColor blackColor]; 
    self.navigationItem.leftBarButtonItem.tintColor = [UIColor greenColor]; 
    //self.navigationItem.rightBarButtonItem.tintColor = [UIColor greenColor]; 
    //self.navigationItem.rightBarButtonItem = nextButton; 
} 

前兩行工作的優良:導航欄的文字爲白色,背景爲黑色。第三次(當我嘗試第四次時),似乎被忽略。

如何使左右欄按鈕的文本顏色爲不同的顏色?我看到蘋果的應用程序在ios7中這樣做。

+0

看IOS 7應用程序後,我想這不能做。 – cdub

回答

1

我不知道,但如果你想不同的顏色應用於欄按鈕,然後只是嘗試獨立應用tintColor並刪除設置tintColornavigationBar行。去嘗試一下。

更新:

它看起來像蘋果不希望我們對欄按鈕不再適用了不同的顏色。來自UINavigationBar的部分屬性的行爲已從iOS 7更改。我已經在我的Answer中給出了描述,你可以檢查它。

+0

你在ios 7中運行,導致上面的文本爲酒吧按鈕是藍色時,我運行上面的代碼 – cdub

+0

@chris:不。我只是給你的提示。 – Bhavin

+0

是的好吧,似乎ios 7不喜歡這個,佈局轉換指南並沒有顯示這是一種可能性 - https://developer.apple.com/library/ios/documentation/UserExperience/Conceptual/TransitionGuide/Bars。 html#// apple_ref/doc/uid/TP40013174-CH8-SW1 – cdub

1

這應該工作。我剛剛測試了一個最小的應用程序。確保在嘗試着色時,實際設置了leftBarButtonItem。

Different tint colors for left and right UINavigationBar items on iOS 7 iPad 3

+0

這是你在哪裏運行的ios 7? – cdub

+0

當然。哦,我只注意到我只在模擬器上測試過。我注意到,尤其是iOS 7中的條形和色調,行爲和錯誤可能在模擬器和設備(甚至是設備之間)中有所不同。馬上回來。 – fzwo

+0

我試過它在設備上;看附件截圖。請原諒顏色的選擇,這是一個測試應用程序,旨在使色彩差異非常明確。 – fzwo

1

我在使用其他答案時遇到了麻煩,但這對我很好。

[self.clearButton setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys: 
             [UIColor redColor], NSForegroundColorAttributeName, 
             nil] forState:UIControlStateNormal]; 

其中clearButton只是對我的左側導航欄項目的引用。使用ios7。

+1

'UITextAttributeTextColor'在iOS7上已棄用,請使用'NSForegroundColorAttributeName'而不是 – auco

+0

,謝謝@auco,現在已修復。 – horsejockey

0

我這樣做是通過設置在AppDelegate中一般的顏色:

UINavigationBar.appearance().tintColor = tintColor 

然後,我會創建單獨的視圖控制器的自定義欄按鈕項並將其設置爲任意leftBarButton或rightBarButton。

let filterStr = NSLocalizedString("FILTER", value: "Filter", comment: "Navigation bar item") 
self.searchButton = UIBarButtonItem.init(title: filterStr, style: .plain, target: self, action: #selector(<someMethod()>)  
self.searchButton?.setTitleTextAttributes([NSForegroundColorAttributeName:UIColor.gray], for: UIControlState.normal) 
self.navigationItem.setLeftBarButton(self.searchButton!, animated: false) 

enter image description here