简体   繁体   中英

I can't add a barButton Item to a navigationBar

I Embedded a ViewController in a UINavigationController and after that, I added a new ViewController and connected it with a segue to the first ViewController that is embedded in a UINavigationController . then I wanted to add a BarButtonItem to that second view, but when I let go the bar button item in the top of the view next to the title, it inserts it at the button, and when I run the app it only shows the back button to the first screen and the title. (see the picture of how it looks when running and how it looks after inserting it at the top) 在iPad模拟器中运行应用程序 将barbuttonitem插入顶部后界面构建器的外观

I hope you understood the question and you can help me!

thanks in advance!

Benji

write this code in your viewDidLoad Method of controller where you want to add button

    let navBtn = UIButton(type: .custom)
    navBtn.setImage(UIImage(named: "side_drawer"), for: .normal)
    navBtn.frame = CGRect(x: 0, y: 0, width: 40, height: 100)
    navBtn.addTarget(self, action: #selector(openMessagingThread(_:)), for: .touchUpInside)
    let rightNavBarItem = UIBarButtonItem(customView: navBtn)
    self.navigationItem.setRightBarButton(rightNavBarItem, animated: true)

//Action for barButton

   @objc
func openMessagingThread(_ sender: UIButton)  {

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM