[英]How to add right button in the navigation bar?
I have a question to add a right button in navigation bar..我有一个问题要在导航栏中添加一个右键按钮..
I have two views: View A and View B我有两个视图:视图 A 和视图 B
I add a navigation bar to view A, after I used self.navigationController!.pushViewController
to show view B.在使用
self.navigationController!.pushViewController
显示视图 B 之后,我添加了一个导航栏来显示视图 A。
That show a back button in the left of navigation bar of view B automatic, it is good.在视图B自动导航栏的左侧显示一个后退按钮,这很好。 but now I want to add a button in the right of navigation bar of view B.. I have tried some answers, but it doesn't work... I have tried answers likes : 1) https://www.hackingwithswift.com/example-code/uikit/how-to-add-a-bar-button-to-a-navigation-bar 2) http://blog.apoorvmote.com/add-multiple-bar-button-item-navigation-bar/?lang=fr
但现在我想在视图 B 的导航栏右侧添加一个按钮..我尝试了一些答案,但它不起作用......我尝试过以下答案:1) https://www.hackingwithswift。 com/example-code/uikit/how-to-add-a-bar-button-to-a-navigation-bar 2) http://blog.apoorvmote.com/add-multiple-bar-button-item-navigation -bar/?lang=fr
Could you help me, thank you !能不能帮帮我,谢谢!
The Swift version of Vahan Babayan's answer, as you seem to use this language, is: Vahan Babayan 的答案的 Swift 版本,正如您似乎使用这种语言一样,是:
let rightButtonItem = UIBarButtonItem.init(
title: "Title",
style: .Done,
target: self,
action: "rightButtonAction:"
)
self.navigationItem.rightBarButtonItem = rightButtonItem
The following method will be called on self:将在 self 上调用以下方法:
func rightButtonAction(sender: UIBarButtonItem)
Note that all this can be set graphically using a Storyboard , by dragging a Bar Button Item to your Navigation Item and right-clicking to set a target-action.请注意,所有这些都可以使用 Storyboard以图形方式设置,方法是将 Bar Button Item 拖动到您的 Navigation Item 并右键单击以设置目标操作。
A small update since Swift 3 and 4 are out: the compiler can now check selector names, preventing typos when setting up target-action programatically. Swift 3 和 4 发布后的一个小更新:编译器现在可以检查选择器名称,防止在以编程方式设置目标操作时出现拼写错误。 So one should really use:
所以一个人真的应该使用:
let rightButtonItem = UIBarButtonItem.init(
title: "Title",
style: .Done,
target: self,
action: #selector(rightButtonAction(sender:))
)
You can add the following code to your B controller's viewDidLoad
method.您可以将以下代码添加到 B 控制器的
viewDidLoad
方法中。
UIBarButtonItem *rightButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"Title"
style:UIBarButtonItemStyleDone
target:self
action:@selector(rightButtonAction:)];
self.navigationItem.rightBarButtonItem = rightButtonItem;
- (void)viewDidLoad {
[super viewDidLoad];
UIBarButtonItem *rightBtn = [[UIBarButtonItem alloc]initWithTitle:@"Right Button" style:UIBarButtonItemStyleDone target:self action:@selector(rightBtnClick)];
self.navigationItem.rightBarButtonItem = rightBtn;
}
-(void)rightBtnClick{
// code for right Button
}
Add below code in viewDidLoad Method在 viewDidLoad 方法中添加以下代码
UIBarButtonItem *flipButton = [[UIBarButtonItem alloc]
initWithTitle:@"Flip"
style:UIBarButtonItemStyleBordered
target:self
action:@selector(flipView:)];
self.navigationItem.rightBarButtonItem = flipButton;
This adds a button to the right hand side with the title Flip, which calls the method:这会在右侧添加一个标题为 Flip 的按钮,该按钮调用该方法:
-(IBAction)flipView
Swift code to add a navigation bar button item:添加导航栏按钮项的Swift代码:
Method 1 (When you wanna use bar button system item)方法一(当你想使用条形按钮系统项时)
navigationItem.leftBarButtonItem = UIBarButtonItem(barButtonSystemItem: .add, target: self, action: #selector(addTapped))
Method 2 (When you wanna give your own title to button)方法 2 (当你想给自己的按钮标题时)
navigationItem.rightBarButtonItem = UIBarButtonItem(title: "Add", style: .plain, target: self, action: #selector(addTapped))
From iOS 5 onwards, it allows you to add more than 1 button on either side of a navigation bar.从 iOS 5 开始,它允许您在导航栏的任一侧添加 1 个以上的按钮。 Something like this:
像这样的东西:
let add = UIBarButtonItem(barButtonSystemItem: .add, target: self, action: #selector(addTapped))
let display = UIBarButtonItem(title: "Display", style: .plain, target: self, action: #selector(playTapped))
navigationItem.rightBarButtonItems = [add, display]
Try this code for Swift UI :试试这个Swift UI代码:
.navigationBarItems(
leading:
HStack {
Button("About") {
print("About tapped!")
}
Button("Call") {
print("Call tapped!")
}
},
trailing:
HStack {
Button("Settings") {
print("Settings tapped!")
}
Button("Contacts") {
print("Contacts tapped!")
}
}
)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.