94

I have a Navigation Bar in a view, and I want to change its Navigation Item title programmatically.

How is this done?

IPS Brar
  • 346
  • 1
  • 14
Doug Null
  • 7,989
  • 15
  • 69
  • 148

12 Answers12

227

In your UIViewController

self.navigationItem.title = @"The title";
arclight
  • 5,299
  • 1
  • 22
  • 26
34

Swift 3 Version:

If you use a navigation bar without navigation controller, then you can try this:

navigationBar.topItem?.title = "Your Title"

Hope for help.

Jerome
  • 2,114
  • 24
  • 24
11

In viewDidLoad

  self.title = @"Title";
Madhu
  • 2,565
  • 2
  • 25
  • 32
  • 3
    My two cents :) This works, although if the vewcontroller is part of a toolbarcontroller. Then the toolbaritem's title is updated as well. – Prakash Raman Sep 16 '17 at 09:28
7

From Apple Documentation:

The most common way to use a navigation bar is with a navigation controller. You can also use a navigation bar as a standalone object in your app.

So If you have a UINavigationController, all what you have to do to set the title of the navigation bar (as explained in all previous answers)

self.navigationItem.title = @"title";

But If you have a standalone navigation bar that is created programmatically within an object of UIViewController, You have to set the initial appearance of the navigation bar by creating the appropriate UINavigationItem objects and adding them to the navigation bar object stack i.e.

  1. Create an instance of navigation bar (UINavigationBar)
  2. Create an instance of navigation bar item (UINavigationItem) that manages the buttons and views to be displayed in a UINavigationBar object. Note that you set the title at this step.
  3. (Optional Step) Add right/left buttons (instances of UIBarButtonItem) to the navigation bar item.

Sample of Objective-C Code for the mentioned steps:

UINavigationBar* navbar = [[UINavigationBar alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 64)];

/* Create navigation item object & set the title of navigation bar. */
UINavigationItem* navItem = [[UINavigationItem alloc] initWithTitle:self.shoppingItem.name];

/* Create left button item. */
UIBarButtonItem* cancelBtn = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemCancel target:self action:@selector(onTapCancel:)];
navItem.leftBarButtonItem = cancelBtn;

/* Create left button item. */
UIBarButtonItem* doneBtn = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:@selector(onTapDone:)];
navItem.rightBarButtonItem = doneBtn;

/* Assign the navigation item to the navigation bar.*/
[navbar setItems:@[navItem]];

/* add navigation bar to the root view.*/
[self.view addSubview:navbar];

For Swift version of the standalone navigation bar, check out this answer.

Community
  • 1
  • 1
Wael Showair
  • 3,092
  • 2
  • 22
  • 29
7

Write the below code in viewDidLoad or may be better would be under initWithNibName:

  self.navigationItem.title = @"title";

Thanks.

Parth Bhatt
  • 19,381
  • 28
  • 133
  • 216
Nikhil Bansal
  • 1,545
  • 13
  • 29
6

You can also use

[self.navigationController.navigationBar.topItem setTitle:@"Titlet"];
Vinodh
  • 5,262
  • 4
  • 38
  • 68
Surjeet Rajput
  • 1,251
  • 17
  • 24
  • You can, but why would you? The view controller doesn't know that's it's on top, so this seems likely to cause confusion. `self.title` is much more precise. – Robin Daugherty Oct 25 '21 at 15:55
6

In your UIViewController's viewDidLoad

self.navigationItem.title = "The title";  //In swift 4

or

you can just

self.title = "The title"

will do the trick

you can also put the above statement in the app delegate to replicate globally.

NickCoder
  • 1,504
  • 2
  • 23
  • 35
3

This code did not work for me

self.navigationItem.title = @"Title here";

But this worked.

self.title = "Title Name"
Stephen King
  • 581
  • 5
  • 18
  • 31
Prasad A
  • 77
  • 7
0

Use it in ViewDidLoad method

Format :

@property(nonatomic,readonly,retain) UINavigationItem *navigationItem; // Created on-demand so that a view controller may customize its navigation appearance.

Example :

self.navigationItem.title = @"Title here";
Jayprakash Dubey
  • 35,723
  • 18
  • 170
  • 177
0

very important note : make sure you make the changes that mentioned above in parent view Controller's viewDidLoad method, or parents navigation bar in storyboard

smoothumut
  • 3,423
  • 1
  • 25
  • 35
0

Sometimes you might get a case where you are updating the title and it is not causing any effect, this can be when you have a custom view on the topItem therefore you might want to update it like this: (self.navigationController?.navigationBar.topItem?.titleView as? UILabel)?.text = "My Happy New Title"

0

Swift:

self.navigationItem.title = "The title;

Objective c

self.navigationItem.title = @"The title";

SwiftUI

.navigationBarTitle("Your Title Here")