2014-01-10 75 views
0

這裏是正在建立,它是接近的顏色,我需要:通過外觀代理設置的UIBarButtonItem的文本顏色

NSDictionary *barButtonItemTitleAttributesEnabled = @{ 
               NSFontAttributeName:[UIFont MRFontLightOfSize:17], 
               NSForegroundColorAttributeName:[UIColor whiteColor] 
               }; 
NSDictionary *barButtonItemTitleAttributesDisabled = @{ 
                 NSFontAttributeName:[UIFont MRFontLightOfSize:17], 
                 NSForegroundColorAttributeName:[UIColor colorWithWhite:1.0f alpha:0.25f] 
                 }; 

[[UIBarButtonItem appearance] setTitleTextAttributes:barButtonItemTitleAttributesEnabled forState:UIControlStateNormal]; 
[[UIBarButtonItem appearance] setTitleTextAttributes:barButtonItemTitleAttributesDisabled forState:UIControlStateDisabled]; 

但我更喜歡以某種方式設置禁用文本顏色相同的顏色選擇狀態,有沒有辦法做到這一點與外觀代理調用?

還試圖這樣:

NSDictionary *barButtonItemDisabled = [[UIBarButtonItem appearance] titleTextAttributesForState:UIControlStateSelected]; 
[[UIBarButtonItem appearance] setTitleTextAttributes:barButtonItemTitleAttributesEnabled forState:UIControlStateNormal]; 
[[UIBarButtonItem appearance] setTitleTextAttributes:barButtonItemDisabled forState:UIControlStateDisabled]; 
+0

就是這裏了您的問題,你想要得到的文本顏色當你按下按鈕時系統設置?所以如果你的按鈕是紅色,當你按下它使它有點不同紅色?只是想在證明答案之前確認我的理解 – random

+0

如果你想要相同的顏色,然後通過相同的顏色。 – rmaddy

回答

4

通過去除TextTitleAttribute以上代碼和設置在工具欄tintColor外觀代理解決:

[[UIToolbar appearance] setTintColor:[UIColor whiteColor]]; 

,這使得它使剛剛啓用或禁用的按鈕將它們設置爲正確的顏色。

1

如果你想給不同的顏色爲不同的項目,那麼你應該使用

UIBarButtonItem *doneButton = [[UIBarButtonItem alloc] 
            initWithTitle:@"Done" style:UIBarButtonItemStylePlain 
            target:self action:@selector(done)]; 

[doneButton setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys: [UIColor whiteColor], NSForegroundColorAttributeName,nil] forState:UIControlStateNormal]; 

否則你應該只是簡單的寫

[toolBar setTintColor:[UIColor whiteColor]]; 
相關問題