I have a Navigation Bar in a view, and I want to change its Navigation Item title programmatically.
How is this done?
I have a Navigation Bar in a view, and I want to change its Navigation Item title programmatically.
How is this done?
In your UIViewController
self.navigationItem.title = @"The title";
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.
In viewDidLoad
self.title = @"Title";
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.
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.
Write the below code in viewDidLoad
or may be better would be under initWithNibName:
self.navigationItem.title = @"title";
Thanks.
You can also use