Swift 3.0 Adding a Right Button to Navigation Bar

前端 未结 6 962
夕颜
夕颜 2020-12-29 04:00

I have added a navigation bar to the top of a view controller. I am trying to control whether a button is visible based a condition, but I am having trouble adding the butto

6条回答
  •  半阙折子戏
    2020-12-29 04:13

    tested in Xcode 10.2, swift 5.0; First, I have have embedded my ViewController in UINavigationController in IB. Then in ViewDidLoad include these lines

    self.title = "orange"
     self.navigationItem.rightBarButtonItem = UIBarButtonItem(barButtonSystemItem: .add, target: self, action: #selector(changeLayout)).
    

    Note - accessing title, or adding button through navigation controller did not work. For example : setting title - Self.navigationcontroller.navigationItem.title did not work ,

提交回复
热议问题