Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

UINavigationBar - Set title programmatically?

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?

like image 261
AveragePro Avatar asked Sep 09 '10 21:09

AveragePro


People also ask

How do you add a title to the navigation bar?

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.

How do I change the navigation bar title color in Swiftui?

To change the color of the navigation bar and the title text, we can use UINavigationBarAppearance, which is available since iOS 13. We hereby make a class Theme with a static method as follows, which will come in handy when we need it for some setup in our View or elsewhere.


2 Answers

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.

like image 187
Greg Avatar answered Oct 06 '22 16:10

Greg


Use

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

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

like image 36
ChrisP Avatar answered Oct 06 '22 17:10

ChrisP