How to add a button to UINavigationBar?

How to add a button to UINavigationBar programmatically?

109089 次浏览

NavigationBar上设置 rightbutton的示例代码。

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;

以上的答案都不错,但我想再补充一些技巧:

如果你想修改后退按钮的标题(导航栏左侧的箭头 y) ,你必须在上一个视图控制器中进行,而不是它将显示的那个。这就像是说: “嘿,如果你在这个视图控制器之上推另一个视图控制器,调用返回按钮“ Back”(或其他) ,而不是默认值。”

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.

如果您想显示一个特殊的符号按钮,可以使用带有值(如 UIBarButtonSystemItemAdd)的形式 initWithBarButtonSystemItem:target:action

记住,这个符号的意义取决于你,但是要注意人机界面指南。使用 UIBarButtonSystemItemAdd 表示删除项目可能会导致应用程序被拒绝。

将自定义按钮添加到导航栏(带有 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];

下面的示例将在右侧的导航栏上显示一个标题为“ Contact”的按钮。它的操作从 viewcontroller 调用一个名为“ contact”的方法。如果没有这一行,右边的按钮是不可见的。

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

enter image description here

为什么不使用以下方法: (来自 在 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];

在 Swift 2中,你会这样做:

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

(不是什么大变化)在 Swift 4/5中,它将是:

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

雨燕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)
}