如何以編程方式向UINavigationBar添加按鈕?如何將一個按鈕添加到UINavigationBar?
109
A
回答
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;
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:)];;
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)
}
相關問題
- 1. 如何添加一個帶有'+'的方形按鈕到uinavigationbar?
- 2. 如何以編程方式將後退按鈕添加到UINavigationBar?
- 3. 如何將多個UIBarButtonItem添加到UINavigationBar?
- 4. 如何添加一個按鈕到wx.MenuBar()?
- 5. 如何添加後退按鈕到自定義的UINavigationBar
- 6. 如何手動添加'後退'按鈕到UINavigationBar?
- 7. 如何將一個按鈕添加到tableview的最後一行?
- 8. 在UINavigationbar中添加完成按鈕
- 9. 如何將自定義圖像按鈕添加到iPhone中的UINavigationBar sdk
- 10. 將函數添加到按鈕陣列中的一個按鈕
- 11. 添加一個NSTimer到一個按鈕
- 12. 如何將按鈕添加到JxTable?
- 13. 如何將文本添加到按鈕?
- 14. 如何將按鈕添加到textfield iphone?
- 15. 如何將邊框添加到按鈕?
- 16. 如何將按鈕添加到settings.bundle?
- 17. 如何將動作添加到按鈕?
- 18. 如何將按鈕添加到GoogleMap上
- 19. 如何將segue添加到rowAction按鈕?
- 20. 將圖像添加到UINavigationBar
- 21. 如何將一個按鈕添加到WizardForm中的buttonBar
- 22. 如何將一個按鈕添加到單元格末尾Swift
- 23. 如何將一個按鈕添加到UITableView條目的右側?
- 24. 如何使用as3將一個按鈕添加到舞臺上?
- 25. 如何將一個按鈕添加到表中
- 26. UITableViewCell子類 - 如何將一個按鈕添加到某些行
- 27. 如何添加一些func到按鈕?
- 28. 如何添加一個按鈕作爲
- 29. 將按鈕添加到BrowseFragment
- 30. 將按鈕添加到ListActivity
我得到的樣式警告:參數 - >警告:語義問題:從枚舉類型「UIBarButtonSystemItem」隱式轉換到不同的枚舉類型「UIBarButtonItemStyle」 – pojo 2011-10-12 20:19:37
這應該是initWithBarButtonSystemItem:UIBarButtonSystemItemDone避免了警告。 – JordanC 2011-12-26 09:55:31
在這個例子中,我不明白「酒吧」來自哪裏。什麼是UINavigationItem的默認頂欄屬性? – aneuryzm 2012-06-26 13:57:55