怎么在这里试试这个
进口的UIKit
一流的ViewController:UIViewController的{
override func viewDidLoad() {
super.viewDidLoad()
let addButton = UIBarButtonItem(barButtonSystemItem: UIBarButtonSystemItem.Refresh, target: self, action: "buttonMethod")
navigationItem.leftBarButtonItem = addButton
navigationController?.navigationBar.barTintColor = UIColor.greenColor()
}
func buttonMethod() {
print("Perform action")
}
}
使用自定义栏按钮试试这个
类的ViewController:UIViewController的{
override func viewDidLoad() {
super.viewDidLoad()
let btnName = UIButton()
btnName.setImage(UIImage(named: "imagename"), forState: .Normal)
btnName.frame = CGRectMake(0, 0, 30, 30)
btnName.addTarget(self, action: Selector("action"), forControlEvents: .TouchUpInside)
//.... Set Right/Left Bar Button item
let rightBarButton = UIBarButtonItem()
rightBarButton.customView = btnName
self.navigationItem.rightBarButtonItem = rightBarButton
}
func action() {
print("Perform action")
}
}
你是否试图在导航栏中添加barbutton?如果是的话,添加按钮时应该存在self.navigationbar。 –