2012-12-03 121 views
10

如何將UIBarButtonItem's顏色設置爲綠色?我使用的是iOS 4,沒有色調屬性。請幫幫我。更改UIBarbuttonItems顏色

+0

你試試我的答案?請告訴我,如果你仍然有問題。 –

+0

@MidhunMP:UIBarButton的顏色已更改。但是如何設置標題和動作呢? – user1871678

+0

我編輯了我的答案,請檢查它是否正常工作。 –

回答

14

在iOS中4:

UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom]; 
[button setBackgroundImage:[UIImage imageNamed:@"green.png"] forState:UIControlStateNormal]; 
button.frame=CGRectMake(0.0, 100.0, 60.0, 30.0); 
[button setTitle:@"Green" forState:UIControlStateNormal]; 
[button addTarget:self action:@selector(yourAction) forControlEvents:UIControlEventTouchUpInside]; 
UIBarButtonItem *myButton = [[UIBarButtonItem alloc] initWithCustomView:button]; 

在這裏,你需要這樣一個綠色的形象,要創建與此圖像的自定義按鈕,將其設置爲的UIBarButtonItem的視圖。

在iOS 5中,您可以使用:

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

請檢查這些鏈接,瞭解更多:

  1. UIAppearance Protocol
  2. User interface customization
+0

根據文檔,它可從iOS 5起。他的問題是針對iOS 4.在這裏檢查它.. http://developer.apple.com/library/ios/#documentation/uikit/reference/UIAppearance_Protocol/Reference/Reference.html – iDev

+0

可憐的Midhun。今天錯誤的問題很好的答案:)我討厭這樣的日子:) – rmaddy

+0

@rmaddy:我認爲你是對的:)這對我來說是糟糕的一天:( –

2

請嘗試下面的代碼。

self.navigationController.navigationBar.tintColor = [UIColor greenColor]; 
self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:nil action:NULL]; 
0

我知道這是一個老問題,但在任何情況下使用customView,你可以嘗試:

myButton.customView.tintColor = [UIColor blackColor];