2010-03-23 118 views

回答

289

rightbutton設置爲NavigationBar的示例代碼。

UIBarButtonItem *rightButton = [[UIBarButtonItem alloc] initWithTitle:@"Done" 
    style:UIBarButtonItemStyleDone target:nil action:nil]; 
UINavigationItem *item = [[UINavigationItem alloc] initWithTitle:@"Title"]; 
item.rightBarButtonItem = rightButton; 
item.hidesBackButton = YES; 
[bar pushNavigationItem:item animated:NO]; 

,但通常你將有一個NavigationController,讓你寫:以上

UIBarButtonItem *rightButton = [[UIBarButtonItem alloc] initWithTitle:@"Done" 
    style:UIBarButtonItemStyleDone target:nil action:nil]; 
self.navigationItem.rightBarButtonItem = rightButton; 
+1

我得到的樣式警告:參數 - >警告:語義問題:從枚舉類型「UIBarButtonSystemItem」隱式轉換到不同的枚舉類型「UIBarButtonItemStyle」 – pojo 2011-10-12 20:19:37

+3

這應該是initWithBarButtonSystemItem:UIBarButtonSystemItemDone避免了警告。 – JordanC 2011-12-26 09:55:31

+2

在這個例子中,我不明白「酒吧」來自哪裏。什麼是UINavigationItem的默認頂欄屬性? – aneuryzm 2012-06-26 13:57:55

20

的回答都不錯,但我想割肉出來多帶幾個小竅門:

如果您想要修改後退按鈕的標題(箭頭-Y從導航欄的左側看),您必須在前一個視圖控制器中執行此操作,而不是它將顯示的視圖控制器。這就好像在說:「嘿,如果你把另一個視圖控制器放在這個視圖控制器的頂部,就把後面的按鈕叫做」Back「(或其他),而不是默認的。」

如果要在特殊狀態期間隱藏後退按鈕(例如顯示UIPickerView時),請使用self.navigationItem.hidesBackButton = YES;並記住在離開特殊狀態時將其設回。

如果你想顯示特殊符號按鈕中的一個,使用形式initWithBarButtonSystemItem:target:action與像UIBarButtonSystemItemAdd

值記住,符號的意義是你的,但要小心的人機界面指南。使用UIBarButtonSystemItemAdd來表示刪除一個項目可能會讓你的應用程序被拒絕。

11

將自定義按鈕添加到導航欄(使用buttonItem的圖像並指定操作方法(void)openView {}和)。

UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom]; 
button.frame = CGRectMake(0, 0, 32, 32); 
[button setImage:[UIImage imageNamed:@"settings_b.png"] forState:UIControlStateNormal]; 
[button addTarget:self action:@selector(openView) forControlEvents:UIControlEventTouchUpInside]; 

UIBarButtonItem *barButton=[[UIBarButtonItem alloc] init]; 
[barButton setCustomView:button]; 
self.navigationItem.rightBarButtonItem=barButton; 

[button release]; 
[barButton release]; 
7

下面的示例將在右側的導航欄上顯示一個標題爲「聯繫人」的按鈕。它的動作從視圖控制器調用名爲「contact」的方法。沒有這條線,右邊的按鈕是不可見的。

self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"Contact" 
                      style:UIBarButtonItemStylePlain target:self action:@selector(contact:)];; 

enter image description here

2

爲什麼不使用下列內容: (從Draw custom Back button on iPhone Navigation Bar

// Add left 
UINavigationItem *previousItem = [[UINavigationItem alloc] initWithTitle:@"Back title"]; 
UINavigationItem *currentItem = [[UINavigationItem alloc] initWithTitle:@"Main Title"]; 
[self.navigationController.navigationBar setItems:[NSArray arrayWithObjects:previousItem, currentItem, nil] animated:YES]; 

// set the delegate to self 
[self.navigationController.navigationBar setDelegate:self]; 
3

在斯威夫特2,你會怎麼做:

let rightButton: UIBarButtonItem = UIBarButtonItem(title: "Done", style: UIBarButtonItemStyle.Done, target: nil, action: nil) 
self.navigationItem.rightBarButtonItem = rightButton 
0

SWIFT 3

let cancelBarButton = UIBarButtonItem(title: "Cancel", style: .done, target: self, action: #selector(cancelPressed(_:))) 
    cancelBarButton.setTitleTextAttributes([NSFontAttributeName : UIFont.cancelBarButtonFont(), 
                  NSForegroundColorAttributeName : UIColor.white], for: .normal) 
    self.navigationItem.leftBarButtonItem = cancelBarButton 


    func cancelPressed(_ sender: UIBarButtonItem) { 
     self.dismiss(animated: true, completion: nil) 
    } 
相關問題