How to add a button to UINavigationBar programmatically?
如何以编程方式向UINavigationBar添加一个按钮?
7 个解决方案
#1
291
Sample code to set the rightbutton
on a NavigationBar
.
在导航条上设置right按钮的示例代码。
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];
But normally you would have a NavigationController
, enabling you to write:
但是通常你会有一个导航控制器,让你可以写:
UIBarButtonItem *rightButton = [[UIBarButtonItem alloc] initWithTitle:@"Done"
style:UIBarButtonItemStyleDone target:nil action:nil];
self.navigationItem.rightBarButtonItem = rightButton;
#2
20
The answers above are good, but I'd like to flesh them out with a few more tips:
上面的答案很好,但我想补充一些建议:
If you want to modify the title of the back button (the arrow-y looking one at the left of the navigation bar) you MUST do it in the PREVIOUS view controller, not the one for which it will display. It's like saying "hey, if you ever push another view controller on top of this one, call the back button "Back" (or whatever) instead of the default."
如果您想修改back按钮的标题(箭头-y在导航栏的左边),您必须在以前的视图控制器中进行修改,而不是在它将要显示的视图控制器中进行修改。这就像是在说,“嘿,如果你在这个视图控制器的上面按下另一个视图控制器,就把后退按钮叫做“后退”(或者别的什么),而不是默认的。”
If you want to hide the back button during a special state, such as while a UIPickerView is displayed, use self.navigationItem.hidesBackButton = YES;
and remember to set it back when you leave the special state.
如果您希望在特殊状态下隐藏back按钮,例如在显示UIPickerView时,请使用self.navigationItem。hidesBackButton =是的;当你离开特殊状态时,记得要把它调回来。
If you want to display one of the special symbolic buttons, use the form initWithBarButtonSystemItem:target:action
with a value like UIBarButtonSystemItemAdd
如果您想显示一个特殊的符号按钮,请使用initWithBarButtonSystemItem:target:带有值的操作,如UIBarButtonSystemItemAdd
Remember, the meaning of that symbol is up to you, but be careful of the Human Interface Guidelines. Using UIBarButtonSystemItemAdd to mean deleting an item will probably get your application rejected.
记住,符号的含义取决于您,但是要注意人机界面的指导方针。使用UIBarButtonSystemItemAdd意味着删除一个项目可能会使你的应用程序被拒绝。
#3
11
Adding custom button to navigation bar ( with image for buttonItem and specifying action method (void)openView{} and).
向导航条添加自定义按钮(带有按钮项的图像并指定操作方法(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];
#4
7
The example below will display a button with a title "Contact" on the navigation bar on the right. Its action calls a method named "contact" from the viewcontroller. Without this line the right button is not visible.
下面的示例将在右边的导航条上显示一个标题为“Contact”的按钮。它的操作调用来自viewcontroller的“contact”方法。没有这一行,右按钮是不可见的。
self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"Contact"
style:UIBarButtonItemStylePlain target:self action:@selector(contact:)];;
#5
3
In Swift 2, you would do:
在《Swift 2》中,你会这样做:
let rightButton: UIBarButtonItem = UIBarButtonItem(title: "Done", style: UIBarButtonItemStyle.Done, target: nil, action: nil)
self.navigationItem.rightBarButtonItem = rightButton
#6
2
Why not use the following: (from Draw custom Back button on iPhone Navigation Bar)
为什么不使用以下功能:(从iPhone导航条上的自定义后退按钮)
// 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];
#7
0
swift 3
斯威夫特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
291
Sample code to set the rightbutton
on a NavigationBar
.
在导航条上设置right按钮的示例代码。
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];
But normally you would have a NavigationController
, enabling you to write:
但是通常你会有一个导航控制器,让你可以写:
UIBarButtonItem *rightButton = [[UIBarButtonItem alloc] initWithTitle:@"Done"
style:UIBarButtonItemStyleDone target:nil action:nil];
self.navigationItem.rightBarButtonItem = rightButton;
#2
20
The answers above are good, but I'd like to flesh them out with a few more tips:
上面的答案很好,但我想补充一些建议:
If you want to modify the title of the back button (the arrow-y looking one at the left of the navigation bar) you MUST do it in the PREVIOUS view controller, not the one for which it will display. It's like saying "hey, if you ever push another view controller on top of this one, call the back button "Back" (or whatever) instead of the default."
如果您想修改back按钮的标题(箭头-y在导航栏的左边),您必须在以前的视图控制器中进行修改,而不是在它将要显示的视图控制器中进行修改。这就像是在说,“嘿,如果你在这个视图控制器的上面按下另一个视图控制器,就把后退按钮叫做“后退”(或者别的什么),而不是默认的。”
If you want to hide the back button during a special state, such as while a UIPickerView is displayed, use self.navigationItem.hidesBackButton = YES;
and remember to set it back when you leave the special state.
如果您希望在特殊状态下隐藏back按钮,例如在显示UIPickerView时,请使用self.navigationItem。hidesBackButton =是的;当你离开特殊状态时,记得要把它调回来。
If you want to display one of the special symbolic buttons, use the form initWithBarButtonSystemItem:target:action
with a value like UIBarButtonSystemItemAdd
如果您想显示一个特殊的符号按钮,请使用initWithBarButtonSystemItem:target:带有值的操作,如UIBarButtonSystemItemAdd
Remember, the meaning of that symbol is up to you, but be careful of the Human Interface Guidelines. Using UIBarButtonSystemItemAdd to mean deleting an item will probably get your application rejected.
记住,符号的含义取决于您,但是要注意人机界面的指导方针。使用UIBarButtonSystemItemAdd意味着删除一个项目可能会使你的应用程序被拒绝。
#3
11
Adding custom button to navigation bar ( with image for buttonItem and specifying action method (void)openView{} and).
向导航条添加自定义按钮(带有按钮项的图像并指定操作方法(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];
#4
7
The example below will display a button with a title "Contact" on the navigation bar on the right. Its action calls a method named "contact" from the viewcontroller. Without this line the right button is not visible.
下面的示例将在右边的导航条上显示一个标题为“Contact”的按钮。它的操作调用来自viewcontroller的“contact”方法。没有这一行,右按钮是不可见的。
self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"Contact"
style:UIBarButtonItemStylePlain target:self action:@selector(contact:)];;
#5
3
In Swift 2, you would do:
在《Swift 2》中,你会这样做:
let rightButton: UIBarButtonItem = UIBarButtonItem(title: "Done", style: UIBarButtonItemStyle.Done, target: nil, action: nil)
self.navigationItem.rightBarButtonItem = rightButton
#6
2
Why not use the following: (from Draw custom Back button on iPhone Navigation Bar)
为什么不使用以下功能:(从iPhone导航条上的自定义后退按钮)
// 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];
#7
0
swift 3
斯威夫特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)
}