UINavigationController 上的 iOS rightBarButtonItem 迅速
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/30993386/
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
iOS rightBarButtonItem on UINavigationController in swift
提问by Marcone
I'm trying to put a rightBarButtonItem
on a second view controller of an UINavigationViewController
stack.
我正在尝试将一个rightBarButtonItem
放在UINavigationViewController
堆栈的第二个视图控制器上。
I'm creating and setting the button in viewDidLoad
of the view controller that I want to show. My actual code looks like this:
我正在创建和设置viewDidLoad
要显示的视图控制器的按钮。我的实际代码如下所示:
override func viewDidLoad() {
super.viewDidLoad()
menu_button_ = UIBarButtonItem(image: UIImage(named: "menu"),
style: UIBarButtonItemStyle.Plain ,
target: self, action: "OnMenuClicked:")
self.navigationController!.navigationItem.rightBarButtonItem = menu_button_
}
What am I missing? The button doesn't appear.
我错过了什么?按钮不出现。
回答by liushuaikobe
You should set the menu_button_
as the rightBarButtonItem
of your viewController
rather than the navigationController
.
您应该将 设置menu_button_
为rightBarButtonItem
您的viewController
而不是navigationController
。
Try
尝试
self.navigationItem.rightBarButtonItem = menu_button_
instead of
代替
self.navigationController!.navigationItem.rightBarButtonItem = menu_button_
回答by Abhishek Sharma
try with following code.
it works for me.
尝试遵循code.
它对我有用。
let homeButton : UIBarButtonItem = UIBarButtonItem(title: "LeftButtonTitle", style: UIBarButtonItemStyle.Plain, target: self, action: "")
let logButton : UIBarButtonItem = UIBarButtonItem(title: "RigthButtonTitle", style: UIBarButtonItemStyle.Plain, target: self, action: "")
self.navigationItem.leftBarButtonItem = homeButton
self.navigationItem.rightBarButtonItem = logButton
And if you want to settle out custom image
then please check with apple guidelines on below link.
如果您想安顿下来,custom image
请查看以下链接中的苹果指南。
回答by Dhiman Ranjit
Create an extension of UINavigationItem like -
创建 UINavigationItem 的扩展,如 -
extension UINavigationItem {
func addSettingButtonOnRight(){
let button = UIButton(type: .custom)
button.setTitle("setting", for: .normal)
button.titleLabel?.font = UIFont.systemFont(ofSize: 15.0)
button.layer.cornerRadius = 5
button.backgroundColor = .gray
button.frame = CGRect(x: 0, y: 0, width: 100, height: 25)
button.addTarget(self, action: #selector(gotSettingPage), for: UIControlEvents.touchUpInside)
let barButton = UIBarButtonItem(customView: button)
self.rightBarButtonItem = barButton
}
@objc func gotSettingPage(){
}
}
And call it from viewDidLoad() like -
并从 viewDidLoad() 调用它,例如 -
self.navigationItem.addSettingButtonOnRight()
回答by iOS
In Swift 5
在斯威夫特 5
//For righter button item
let rightBtn = UIBarButtonItem(image: UIImage(named: "rightmenu"), style: .plain, target: self, action: #selector(onClickMethod))//Change your function name and image name here
self.navigationItem.rightBarButtonItem = rightBtn
//self.navigationItem.rightBarButtonItem = [rightBtn, anotherBtn] //If you want to add more buttons add like this
//This is your function
@objc func onClickMethod() {
print("Left bar button item")
}