How to add right button in the navigation bar?

前端 未结 6 637
傲寒
傲寒 2021-02-05 16:40

I have a question to add a right button in navigation bar..

I have two views: View A and View B

I add a navigation bar to view A, after I used self.navigat

6条回答
  •  既然无缘
    2021-02-05 17:19

    Swift code to add a navigation bar button item:

    Method 1 (When you wanna use bar button system item)

    navigationItem.leftBarButtonItem = UIBarButtonItem(barButtonSystemItem: .add, target: self, action: #selector(addTapped))
    

    Method 2 (When you wanna give your own title to button)

    navigationItem.rightBarButtonItem = UIBarButtonItem(title: "Add", style: .plain, target: self, action: #selector(addTapped))
    

    From iOS 5 onwards, it allows you to add more than 1 button on either side of a navigation bar. Something like this:

    let add = UIBarButtonItem(barButtonSystemItem: .add, target: self, action: #selector(addTapped))
    let display = UIBarButtonItem(title: "Display", style: .plain, target: self, action: #selector(playTapped))
    
    navigationItem.rightBarButtonItems = [add, display]
    

提交回复
热议问题