How to programmatically customize the title of a navigation bar?

I have a navigation bar in the view and I want to programmatically change its name for the navigation element.

How it's done?

+73
ios iphone view uinavigationbar
May 27 '11 at 15:13
source share
11 answers

In your UIViewController

self.navigationItem.title = @"The title"; 
+179
May 27 '11 at 15:15
source share

Swift 3 version:

If you use the navigation bar without a navigation controller, you can try the following:

 navigationBar.topItem?.title = "Your Title" 

We hope for help.

+20
Oct 20 '16 at 7:25
source share

In viewDidLoad

  self.title = @"Title"; 
+10
Nov 16 '13 at 7:27
source share

From Documentation for Apple :

The most common way to use the navigation bar is through the navigation controller. You can also use the navigation bar as a separate object in your application.

So, if you have a UINavigationController, all 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 created programmatically inside the UIViewController object, you must set the initial view of the navigation bar by creating the corresponding UINavigationItem objects and adding them to the navigation bar object stack, i.e.

  • Create an instance of the navigation bar (UINavigationBar)
  • Create an instance of the navigation bar element (UINavigationItem) that controls the buttons and views that will be displayed in the UINavigationBar. Please note that you are setting the title in this step.
  • (Optional step) Add right / left buttons (instances of UIBarButtonItem) to the navigation bar item.

Example Objective-C Code for the specified 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 the Swift standalone navigation bar version, select this answer .

+7
Dec 01 '15 at 17:09
source share

Enter the code below in viewDidLoad or maybe better in initWithNibName:

  self.navigationItem.title = @"title"; 

Thank.

+6
Mar 22 '12 at 5:18
source share

You can also use 👇🏻

 [self.navigationController.navigationBar.topItem setTitle:@"Titlet"]; 
+5
Mar 30 '16 at 11:47
source share

This code does not work for me

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

But it worked.

 self.title = "Title Name" 
+1
Apr 30 '18 at 16:23
source share

In your UIViewController viewDidLoad

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

will do his job

You can also put the above statement in the delegate of the global replication application.

+1
Mar 21 '19 at 5:33
source share

Use it in the 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"; 
0
Nov 05 '14 at 10:34
source share

It is very important to note: make sure that you make the changes mentioned above in the parent view. ViewDidLoad controller type or parent navigation bar in storyboard

0
Dec 04 '15 at 12:47
source share

Sometimes you can get the case when you update the title, and it does not cause any effect, it can be when you have a custom view in topItem , so you can update it as follows: (self.navigationController?.navigationBar.topItem?.titleView as? UILabel)?.text = "My Happy New Title"

0
Aug 31 '16 at 13:25
source share



All Articles