79

I have a tab bar application with a different view on each tab. Each view has a UINavigationBar with title set on Interface Builder. I want to change the title based on a clause in the ViewDidLoad method, so if x { change the title }.

I have tried self.title = @"title", but this changes the title of the tab bar item itself.

So, how is this done?

Cœur
  • 37,241
  • 25
  • 195
  • 267
AveragePro
  • 1,081
  • 2
  • 9
  • 12

11 Answers11

214

I've set the title programmatically using code something like this:

navBar.topItem.title = @"title";

where navBar is declared as an IBOutlet UINavigationBar linked to the navigation bar in interface builder. This worked in my app; however, I was not using a tab bar.

If navBar.topItem is the tab bar item, I don't see a way for you to change the title that appears on the navigation bar without also changing the title on the tab bar item, since the navBar's topItem and the tab bar item is the same object.

Cœur
  • 37,241
  • 25
  • 195
  • 267
Greg
  • 33,450
  • 15
  • 93
  • 100
  • 3
    Worked perfectly, knew I had forgot to do something; and that was link the IBOutlet UINavigationBar to the bar itself. The title and tab text are independent of each other, btw. – AveragePro Sep 09 '10 at 22:31
  • This has a problem, when you have a Back button it changes the title of the back button instead of the navigation bar, check for the response from @CrisP. – clopez Aug 23 '12 at 16:47
  • The question was asking about a view that has a UINavigationBar, which is different than a UINavigationController that has view controllers pushed onto it. ChrisP's answer is certainly the right answer in the context of a UINavigationController, but not for the case discussed in the question. – Greg Jul 17 '13 at 20:49
  • 1
    self.navigationItem.title = @"title"; i believe is the simplest-easiest way – Umit Kaya Jul 10 '15 at 06:24
72

Use

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

as setting navBar.topItem.title will not work in all circumstances.

Marcus Adams
  • 53,009
  • 9
  • 91
  • 143
ChrisP
  • 9,796
  • 21
  • 77
  • 121
  • 4
    NB can only used with UINavigationBar associated with a view controller. From the docs ": This is a unique instance of UINavigationItem created to represent the view controller when it is pushed onto a navigation bar. The first time you access this property, the UINavigationItem is created. Therefore, you shouldn’t access this property if you are not using a navigation controller" – wuf810 Feb 03 '12 at 11:48
25

Use this :

    CGRect navBarFrame = CGRectMake(0, 0, self.tableView.frame.size.width, 44.0);
    UINavigationBar *navBar = [[UINavigationBar alloc] initWithFrame:navBarFrame];

    UINavigationItem *navItem = [UINavigationItem alloc];
    navItem.title = @"Your Title";

    [navBar pushNavigationItem:navItem animated:false];

[self.view addSubView:navBar]; 

or

self.tableView.tableHeaderView = navBar; etc
Muzammil
  • 1,529
  • 1
  • 15
  • 24
11

If the class is a type of UIViewController, then you can set title as given in the viewDidLoad method.

    [self setTitle:@"My Title"];
Augustine P A
  • 5,008
  • 3
  • 35
  • 39
  • This is just, what was causing his problem: it changes the tab bar item's title as well, which was NOT appreciated. – Lepidopteron Mar 23 '17 at 15:56
7

In ViewDidLoad of your ViewController.m just write,

self.navigationItem.title=@"Hello World";

Here is the Output:

enter image description here

iAnkit
  • 1,940
  • 19
  • 25
7

Create IBOutlet of UINavigationBar

navigationBar.topItem.title = @"Title";

Hope this helps.

Parth Bhatt
  • 19,381
  • 28
  • 133
  • 216
Shyam
  • 71
  • 1
  • 1
4

I used the following:

self.navigationController.navigationBar.topItem.title = @"Title";

However, I also had to call it in a dispatch block as it wouldn't change the title when called within the viewDidLoad w/o it.

Tod Cunningham
  • 3,691
  • 4
  • 30
  • 32
2

Note that in order for the title to show up AT ALL the Navigation Controller's delegate MUST be set to the navigation bar itself or the title will NEVER show!

Mike
  • 21
  • 3
2

I achieved this way in Swift 5

override func viewDidLoad() {
    super.viewDidLoad()
    navigationItem.title = "Title"
}
1

You can also just set the title in the ViewDidLoad under your ViewController or TableViewController using

_title = @"Title";

or

self.title = @"Title";
donmiller
  • 141
  • 1
  • 5
0

Try this,

self.navigationItem.title = @"Your title";
Abhishek Gurjar
  • 7,426
  • 10
  • 37
  • 45
Kiran eldho
  • 224
  • 1
  • 8