user3626407
user3626407

Reputation: 287

How to set right navigation bar button title

How do I programatically set my right navigation bar button's title. I've tried

[self.navigationItem.rightBarButtonItem setTitle:@"Test"];

and have also tried creating an outlet for the button and then trying to assign it a value.

self.rightBarButton.titleLabel.text = @"Test";

Upvotes: 10

Views: 14829

Answers (6)

ShaileshAher
ShaileshAher

Reputation: 823

For Single Navigation Button

self.navigationItem.rightBarButtonItem = UIBarButtonItem(barButtonSystemItem: .add, target: self, action: #selector(yourFunctionWithObjcKeyword))

For Multiple Navigation Buttons

let add = UIBarButtonItem(barButtonSystemItem: .add, target: self, action: #selector(yourFunctionWithObjcKeyword))

let edit = UIBarButtonItem(barButtonSystemItem: .edit, target: self, action: #selector(yourFunctionWithObjcKeyword))

self.navigationItem.rightBarButtonItems = [add, edit]

Note: you must embed your controller inside UINavigationController to make above code work. In case of UITableViewController and UICollectionViewController, this code doesn't work.

Upvotes: 0

david72
david72

Reputation: 7307

This works for swift 3:

navigationItem.rightBarButtonItem?.title = "Test"

But the button has to be of custom type, otherwise it doesn't work.

Upvotes: 0

Naresh Maharaj
Naresh Maharaj

Reputation: 169

This is the simple option for Swift 3:

let rightButton = self.editButtonItem
rightButton.title = "My Button"
self.navigationItem.rightBarButtonItem = rightButton

Upvotes: 1

Zaid Pathan
Zaid Pathan

Reputation: 16820

For Swift use this,

self.navigationItem.rightBarButtonItem = UIBarButtonItem(title: "Title", style: UIBarButtonItemStyle.Plain, target: self, action: Selector("methodName"))

Set Color of Title using this,

self.navigationItem.rightBarButtonItem?.tintColor = UIColor.whiteColor()

Upvotes: 7

Harjot Singh
Harjot Singh

Reputation: 6927

Please try this, it works:

    self.navigationItem.rightBarButtonItem.title = @"New Title";

Upvotes: 4

Sviatoslav Yakymiv
Sviatoslav Yakymiv

Reputation: 7935

Try this:

self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"Test" style:UIBarButtonItemStylePlain target:target action:action];

Upvotes: 9

Related Questions