How to programmatically set action for barButtonItem in swift 3?

后端 未结 12 1547
鱼传尺愫
鱼传尺愫 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")
    }
    

提交回复
热议问题