2013-10-17 19 views
5

我的UIBarButtonItems在我的應用程序中使用淺色背景,並且我想用較暗的色彩來代替標準的白色。iOS 6,如何着色UIBarButtonItem白色圖標?

像這樣:

enter image description here

我能夠使用

[[UIBarButtonItem appearance] setTitleTextAttributes:@{UITextAttributeTextColor: [UIColor blackColor]} forState:UIControlStateNormal];, 

進行着色的文字,但我無法弄清楚如何改變圖標的​​顏色。

有沒有可能這樣做?

+0

可能重複的[我可以有一個UIBarButtonItem與彩色圖像?](http://stackoverflow.com/questions/1835260/can-i-have-a-uibarbuttonitem-with-a-colored-image) –

+0

+1。從未注意到這件事。 – Bhavin

回答

0

使用以下代碼可能會對您的情況有所幫助。

[myBarButton_name setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys: [UIColor yellowColor], UITextAttributeTextColor,nil] forState:UIControlStateNormal]; 
+0

我還沒有測試過,但是1:我相信它只會改變文字顏色(不是圖標的顏色),因爲它和我做的基本一樣,用不同的方式寫出來; 2:我寧願進行全局更改,而不是逐個按鈕。 – Guilherme

+0

你的意思是你想改變UIBarButtonItem的顏色? – iPatel

+0

@Guilherme http://stackoverflow.com/questions/4518617/setting-bar-button-item-color-in-a-custom-navigation-bar和http://stackoverflow.com/questions/13677703/change-uibarbuttonitems - 顏色 – iPatel

1

我相信這會在導航欄中工作:

[[UINavigationBar appearance] setTintColor:[UIColor blackColor]]; 

[[UIBarButtonItem appearance] setTintColor:[UIColor blackColor]]; 

和當然,你也可以改變的故事板選項(而不是在我的Mac 整個色調,這樣可以」不要告訴你的確切位置)

3

與您的自定義圖像創建UIButton,然後創建一個與UIButton作爲自定義視圖UIBarButtonItem

UIImage *buttonImage = [UIImage imageNamed:@"image"]; // Tint this image programmatically 
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom]; 
[button setImage:buttonImage forState:UIControlStateNormal]; 
button.frame = CGRectMake(0.0, 0.0, buttonImage.size.width, buttonImage.size.height); 
[button addTarget:self action:@selector(action:) forControlEvents:UIControlEventTouchUpInside]; 

UIBarButtonItem *barButtonItem = [[[UIBarButtonItem alloc] initWithCustomView:button] autorelease]; 

請注意,這主要針對iOS 6及以下版本。在iOS 7+中,您可以通過tintColor免費獲得此行爲。