ios 如何向 UINavigationBar 添加按钮?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2504351/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-30 17:02:55  来源:igfitidea点击:

How to add a button to UINavigationBar?

iosiphonexcodeinterface-builderuinavigationbar

提问by RexOnRoids

How to add a button to UINavigationBar programmatically?

如何以编程方式向 UINavigationBar 添加按钮?

回答by Mads Mob?k

Sample code to set the rightbuttonon a NavigationBar.

示例代码来设置rightbuttonNavigationBar

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:

但通常你会有一个NavigationController,使你能够写:

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

回答by Amagrammer

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."

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

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.

如果您想在特殊状态下隐藏后退按钮,例如在显示 UIPickerView 时,请使用self.navigationItem.hidesBackButton = YES;并记住在离开特殊状态时将其设置回来。

If you want to display one of the special symbolic buttons, use the form initWithBarButtonSystemItem:target:actionwith a value like UIBarButtonSystemItemAdd

如果要显示特殊符号按钮之一,请使用initWithBarButtonSystemItem:target:action具有以下值的表单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 意味着删除项目可能会导致您的应用程序被拒绝。

回答by Oleh Kudinov

Adding custom button to navigation bar ( with image for buttonItem and specifying action method (void)openView{} and).

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

回答by Tibidabo

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”的方法。如果没有这一行,则右侧按钮不可见。

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

enter image description here

在此处输入图片说明

回答by NRR

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

(Not a major change) In Swift 4/5, it will be:

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

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

回答by Joe

Why not use the following: (from Draw custom Back button on iPhone Navigation Bar)

为什么不使用以下内容:(来自iPhone Navigation Bar 上的 Draw custom Back button

// 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];

回答by venky

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