How to programmatically set action for barButtonItem in swift 3?

后端 未结 12 1437
鱼传尺愫
鱼传尺愫 2020-12-23 16:17

Here is what I used previously,

var barButtonItem = UIBarButtonItem(image: backImgs, style: UIBarButtonItemStyle.plain, target: self, action: Selector(\"menu         


        
相关标签:
12条回答
  • 2020-12-23 16:36

    In Swift 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
    
    //For left bar button item
    let leftBtn = UIBarButtonItem(image: UIImage(named: "rightmenu"), style: .plain, target: self, action: #selector(onClickMethod)) //Change your function name and image name here
    self.navigationItem.leftItemsSupplementBackButton = true
    self.navigationItem.leftBarButtonItem = leftBtn
    //self.navigationItem.leftBarButtonItems = [leftBtn, anotherBtn] //If you want to add more buttons add like this
    
    //This is your function name
    @objc func onClickMethod() {
        print("Left bar button item")
    }
    
    0 讨论(0)
  • 2020-12-23 16:38

    One line of code on Swift 3 for iOS 10.1:

    navigationController?.navigationBar.topItem?.rightBarButtonItem = UIBarButtonItem(title: "Add", style: .plain, target: self, action: nil)
    
    0 讨论(0)
  • 2020-12-23 16:38

    In Swift 3, you can add UIBarButtonItem like that,

    let addButton = UIBarButtonItem(image:UIImage(named:"your_icon_name"), style:.plain, target:self, action:#selector(YourControllerName.buttonAction(_:)))
    addButton.tintColor = UIColor.white
    self.navigationItem.rightBarButtonItem = addButton
    

    And handle button action like that,

    func buttonAction(_ sender: UIBarButtonItem) {
    
    }
    

    Hope it helps.

    0 讨论(0)
  • 2020-12-23 16:40

    create an extension for barbutton item.

     extension UINavigationItem {
        func addSettingButtonOnRight(){
           let button = UIButton(type: .Custom)
           button.setTitle("setting", forState: .Normal)
           button.titleLabel?.font = UIFont.systemFontOfSize(15.0)
           button.layer.cornerRadius = 5
           button.backgroundColor = UIColor.grayColor()
           button.frame = CGRect(x: 0, y: 0, width: 100, height: 25)
           button.addTarget(self, action: #selector(gotSettingPage), forControlEvents: UIControlEvents.TouchUpInside)
           let barButton = UIBarButtonItem(customView: button)
    
           self.rightBarButtonItem = barButton
       }
    
       func gotSettingPage(){
    
       }
     }
    

    And call it from viewDidLoad()

     self.navigationItem.addSettingButtonOnRight()
    
    0 讨论(0)
  • 2020-12-23 16:44

    ex:-

    navigationItem.rightBarButtonItem = UIBarButtonItem(title: "Add", style: .plain, target: self, action: #selector(addTapped))
    
    0 讨论(0)
  • 2020-12-23 16:44

    for Swift 4 add in viewDidLoad:

    navigationItem.rightBarButtonItem = UIBarButtonItem(
        barButtonSystemItem: UIBarButtonSystemItem.add, 
        target: self, 
        action: #selector(addTransaction)
    )
    
    0 讨论(0)
提交回复
热议问题