How to add the right button in the navigation bar?

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

I have two views: View A and View B

I add a navigation bar to view A after I used self.navigationController!.pushViewController to display view B.

This shows the back button on the left side of navigation bar B automatically, which is good. but now I want to add a button on the right side of the navigation bar B. I tried some answers, but it doesn’t work ... I tried the answers like: 1) https://www.hackingwithswift.com/example-code/uikit/how -to-add-a-bar-button-to-a-navigation-bar 2) http://blog.apoorvmote.com/add-multiple-bar-button-item-navigation-bar/?lang=fr

Could you help me, thanks!

+8
ios uiviewcontroller swift uinavigationbar
source share
5 answers

A quick version of Vahan Babayan's answer, as you seem to be using this language, is this:

 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 of this can be set graphically using the storyboard by dragging the bar panel item into the navigation item and right-clicking to set the target action.


A minor update since Swift 3 and 4: the compiler can now check selector names, preventing typos when programming the target programmatically. Therefore, you really need to use:

 let rightButtonItem = UIBarButtonItem.init( title: "Title", style: .Done, target: self, action: #selector(rightButtonAction(sender:)) ) 
+22
source share

You can add the following code to your B method viewDidLoad .

 UIBarButtonItem *rightButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"Title" style:UIBarButtonItemStyleDone target:self action:@selector(rightButtonAction:)]; self.navigationItem.rightBarButtonItem = rightButtonItem; 
+4
source share
 - (void)viewDidLoad { [super viewDidLoad]; UIBarButtonItem *rightBtn = [[UIBarButtonItem alloc]initWithTitle:@"Right Button" style:UIBarButtonItemStyleDone target:self action:@selector(rightBtnClick)]; self.navigationItem.rightBarButtonItem = rightBtn; } -(void)rightBtnClick{ // code for right Button } 
+2
source share

Add below code to viewDidLoad method

 UIBarButtonItem *flipButton = [[UIBarButtonItem alloc] initWithTitle:@"Flip" style:UIBarButtonItemStyleBordered target:self action:@selector(flipView:)]; self.navigationItem.rightBarButtonItem = flipButton; 

This adds a button on the right side called Flip, which calls the method:

 -(IBAction)flipView 
0
source share

Quick code to add a navigation bar button element:

Method 1 (If you want to use a button bar item)

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

Method 2 (If you want to assign your title to a button)

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

Starting with iOS 5, it allows you to add more than one button on each side of the navigation bar. Something like that:

 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] 
0
source share

All Articles