user2262304 user2262304 - 6 months ago 16x
Swift Question

How to add right button in the navigation bar?

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

to show view B.

That show a back button in the left of navigation bar of view B automatic, it is good. but now I want to add a button in the right of navigation bar of view B..
I have tried some answers, but it doesn't work...
I have tried answers likes :

Could you help me, thank you !

J2b J2b

The Swift version of Vahan Babayan's answer, as you seem to use this language, is:

let rightButtonItem = UIBarButtonItem.init(
      title: "Title", 
      style: .Done, 
      target: self, 
      action: "rightButtonAction:"

self.navigationItem.rightBarButtonItem = rightButtonItem

The following method will be called on self:

func rightButtonAction(sender: UIBarButtonItem)

Note that all this can be set graphically using a Storyboard, by dragging a Bar Button Item to your Navigation Item and right-clicking to set a target-action.