2010-07-21 86 views
1

我需要更改默認的「self.navigationItem.backBarButtonItem」的顏色。要做到這一點從來就創建了一個自定義按鈕和實施這樣的:如何更改默認backBarButtonItem上的顏色/圖像?

- (void)viewDidLoad { 
[super viewDidLoad];  
BackButton *blueSaveButton = [[BackButton alloc] init]; 
UIBarButtonItem *temporaryBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:blueSaveButton]; 
temporaryBarButtonItem.title = @"Tillbaka"; 
self.navigationItem.backBarButtonItem = temporaryBarButtonItem; 
[temporaryBarButtonItem release]; 
[blueSaveButton release]; 
[self gotoLocation]; 

}

但這並沒有影響在所有的按鈕。 那麼如何在不「打破」導航欄的默認/繼承行爲的情況下做到這一點呢?

編輯:原因是該按鈕需要企業品牌,所以默認樣式不會。

BR

回答

1

如果您希望按鈕是藍色的(如完成按鈕),並有標題爲「保存」,你可以使用內置的欄按鈕項目類型之一:

UIBarButtonItem *item = [[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemSave target:self action:@selector(myAction:)] autorelease]; 
+0

那是快。我需要設置自定義顏色或圖像(企業品牌)。這實際上是iPhone開發的一個重大缺點,即這種簡單的任務需要對自定義控件進行大量的定製。 – Christofffer 2010-07-21 10:02:07

+0

在這種情況下,你將不得不使用一個圖像。創建一個'UIButton',使用'setTitle:forState:'和'setBackgroundImage:forState'方法設置標題和背景圖像的屬性,然後創建'UIButtonItem'作爲自定義視圖。據我所知,在按鈕上設置背景色是不夠的。 – Jasarien 2010-07-21 10:10:09

+0

我已經在BackButton控制器中做到了這一點。我畫的推論是NavigationBarItem覆蓋我的自定義控制器。 – Christofffer 2010-07-21 10:23:25