2010-09-30 68 views
7

我有一個選項卡欄應用程序。並在其中一個標籤欄中有一個導航控制器。 我試圖設置一個圖像的UIButton作爲導航欄的右鍵。UIButton作爲UINavigationbar按鈕

UIButton *refreshButton = [[UIButton alloc] init]; 
    [refreshButton setImage:[UIImage imageNamed:@"refresh_icon.png"] forState:UIControlStateNormal]; 
    [refreshButton addTarget:self action:@selector(refreshSection) forControlEvents:UIControlEventTouchUpInside]; 
    UIBarButtonItem *rightButton = [[UIBarButtonItem alloc] initWithCustomView:refreshButton]; 
    //[rightButton setImage:[UIImage imageNamed:@"refresh_icon.png"]]; << DOESN'T WORK EITHER 
    self.navigationItem.rightBarButtonItem = rightButton; 
    [refreshButton release]; 
    [rightButton release]; 

但圖像不顯示。我得到一個不可見的按鈕。

編輯: 我希望按鈕風格是純色或背景是透明的,以便只有圖像顯示。因此,我使用的是一個UIbutton,而不是直接使用UIBarButton。

回答

22

您不必創建UIButton並將其用作UIBarButtonItem中的自定義視圖。您可以直接創建一個UIBarButtonItem與圖像:

UIImage *myImage = [UIImage imageNamed:@"myImage.png"]; 
UIBarButtonItem *button = [[UIBarButtonItem alloc] initWithImage:myImage style:UIBarButtonItemStyleBordered target:self action:@selector(refreshSection)]; 
self.navigationItem.rightBarButtonItem = button;  
[button release]; 

-

更新,根據意見

使用UIImageView作爲UIBarButtonItem自定義視圖:

UIImageView *imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"myImage.png"]]; 
UIBarButtonItem *rightButton = [[UIBarButtonItem alloc] initWithCustomView:imageView]; 
self.navigationItem.rightBarButtonItem = rightButton; 
[imageView release]; 
[rightButton release]; 

-

更新

嘗試最後的辦法,雖然這也直觀地看起來像你想它,我似乎無法得到它來處理水龍頭。雖然目標和行動已經確定。

-

最後更新

我已經得到了在這個問題您最初的代碼了&運行。圖像沒有顯示的原因是因爲你沒有設置按鈕的框架。設置好框架後,圖像會顯示在導航欄中。

這裏是我的代碼段我用&測試:

UIImage *myImage = [UIImage imageNamed:@"paw.png"]; 
UIButton *myButton = [UIButton buttonWithType:UIButtonTypeCustom]; 
[myButton setImage:myImage forState:UIControlStateNormal]; 
myButton.showsTouchWhenHighlighted = YES; 
myButton.frame = CGRectMake(0.0, 0.0, myImage.size.width, myImage.size.height); 

[myButton addTarget:self action:@selector(tapped:) forControlEvents:UIControlEventTouchUpInside]; 

UIBarButtonItem *rightButton = [[UIBarButtonItem alloc] initWithCustomView:myButton]; 
self.navigationItem.rightBarButtonItem = rightButton; 

[rightButton release]; 
[myButton release]; 

注:我UIButtonshowsTouchWhenHighlighted屬性設置爲在視覺上被按下時,它hightlight按鈕。

+0

事情是我想要按鈕樣式是平原或背景是透明的。 UIBarButtonItemStyleBordered或其他任何東西把圖像放在一個盒子裏。這就是爲什麼我使用uibutton – Rupert 2010-09-30 08:04:22

+0

你應該在你的問題中提到。正如你指出的那樣,在我展示的導航欄中添加一個按鈕將始終使用邊框顯示它,即使將其設置爲普通樣式也是如此。我認爲它甚至在文檔中提到。 – 2010-09-30 08:07:57

+0

現在它顯示圖像,但點擊它時沒有任何反應。我試過[rightButton setTarget:self]; [rightButton setAction:@selector(refresh :)]; – Rupert 2010-09-30 08:26:58

1

我終於實現了這個

UIButton *refreshButton = [[UIButton alloc] initWithFrame:CGRectMake(292, 9, 27, 27)]; 
    [refreshButton setImage:[UIImage imageNamed:@"refresh_icon.png"] forState:UIControlStateNormal]; 
    [refreshButton addTarget:self action:@selector(refresh) forControlEvents:UIControlEventTouchUpInside]; 
    [self.navigationController.navigationBar addSubview:refreshButton]; 
    [refreshButton release]; 

現在工作正常,但我真的不知道,如果這是這樣做的正確方法。

+0

雖然這可能有效,但我認爲使用'UIBarButtonItem'更清潔。 – 2010-09-30 09:05:59