21

I'm trying to change my nav bar's title programmatically but the code I'm using won't work.

self.navigationController.navigationBar.tintColor = [UIColor redColor];
self.navigationController.navigationItem.title=@"Hello";

I can manage to change the colour of the bar but not the text.

0

6 Answers 6

50
self.navigationController.navigationBar.topItem.title = @"YourTitle";
Sign up to request clarification or add additional context in comments.

2 Comments

This bit of code works the exact same as Mudit's but it's shorter. Just place it in viewWillAppear and it's perfect
I have both navbar and tabbar and when i was trying to edit nav title programatically either with self.navigationItem.title or self.navigationcontroller.title or self.title, it was changing values in both navbar title and first tab on tabbar. This line does change only nav bar.Just for future reference for people who have same problems :)
27

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

Comments

12

First check the index of current controller on the navigation stack through

 NSLog(@"%@", self.navigationController.viewControllers );

after finding the index of current ViewController let say 1.

UINavigationController *navCon  = (UINavigationController*) [self.navigationController.viewControllers objectAtIndex:1];
    navCon.navigationItem.title = @"Hello";

4 Comments

Thanks, That managed to set the title for the nav bar, but rather than setting the title for just the one page, I'd like it to have a different title for each tab. I've tried using the above fix on two different tabs, but whatever tab I click last, changes the nav bar title to that permanently. So even if I load my other tab, the title doesn't change. Any idea how to fix that?
in viewWillAppear of that controller you can apply the same process.
I m not genious,actually i have faced the same problem in my previous project,that's y i have d solution...
I am getting null for this
7

You can use built in method setTitle now in XCode

[self setTitle:@"title"]
or
self.title=@"title"

inside its viewController.

1 Comment

ah aththada :D maru ahh
1

For swift 2.2

navigationController?.navigationBar.topItem?.title = "Superman title"

Comments

-2

Try with this :

[navigationController setTitle:@"Title"];

1 Comment

Not a great example as navigationController is probably an instance variable that is not being shared here.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.