2014-03-19 57 views
0

我已經創建了三個UIBarButton項目並將它們添加到UINavigation控制器。UIbarButton圖像不顯示?

按鈕做工精細,並顯示出來,如果我使用此代碼設置自己的形象:

[saveSearchButton setImage:[UIImage imageNamed:@"Save Search"]]; 

然後我想讓它這樣,當按鈕被點擊 - 它的形象是更改爲「選擇圖像「

這裏是我是怎麼做的:

[butt setBackgroundImage:[UIImage imageNamed:@"myImage"] forState:UIControlStateSelected style:UIBarButtonItemStylePlain barMetrics:UIBarMetricsDefault]; 

然而,當我點擊它的形象不會改變(雖然它的操作方法被調用)按鈕

我試圖設置其圖像爲正常狀態,像這樣:

[buttonOne setBackgroundImage:[UIImage imageNamed:@"myImage"] forState:UIControlStateNormal style:UIBarButtonItemStylePlain barMetrics:UIBarMetricsDefault]; 

當我使用上面的代碼中,按鈕是不再可見。如何更改按鈕的選定圖像?

這裏是我如何按鈕添加到UINavigationBar的

創建他們的全部代碼...

UIBarButtonItem *buttonOne = [[UIBarButtonItem alloc]initWithTitle:nil style:UIBarButtonItemStylePlain target:self action:@selector(buttonOnePressed)]; 
    UIBarButtonItem *buttonTwo = [[UIBarButtonItem alloc]initWithTitle:nil style:UIBarButtonItemStylePlain target:self action:@selector(buttonTwoPressed)]; 
    UIBarButtonItem *buttonThree = [[UIBarButtonItem alloc]initWithTitle:nil style:UIBarButtonItemStylePlain target:self action:@selector(buttonThreePressed)]; 

添加到陣列

NSArray *barButtonItems = @[buttonOne,buttonTwo,buttonThree,]; 

添加到導航項目

self.navigationItem.rightBarButtonItems = barButtonItems; 

回答

0

請嘗試以下代碼:

UIButton *button = [[UIButton alloc] init]; 

[button setImage:[UIImage imageNamed:@"myImage.png"] forState:UIControlStateNormal]; 
[button addTarget:self action:@selector(buttonOnePressed:) 
    forControlEvents:UIControlEventTouchUpInside]; 

UIBarButtonItem *barButton = [[UIBarButtonItem alloc] 
          initWithCustomView:button]; 

self.navigationItem.rightBarButtonItem = barButton; 
+0

謝謝,但這並不奏效。該按鈕不顯示。 – Tander