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";
Write the below code in viewDidLoad or may be better would be under initWithNibName:
self.navigationItem.title = #"title";
Thanks.
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.
Create an instance of navigation bar (UINavigationBar)
Create an instance of navigation bar item (UINavigationItem) that manages the buttons and views to be displayed in a UINavigationBar object. Note that you set the title at this step.
(Optional Step) Add right/left buttons (instances of UIBarButtonItem) to the navigation bar item.
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.
You can also use 👇🏻
[self.navigationController.navigationBar.topItem setTitle:#"Titlet"];
In your UIViewController's viewDidLoad
self.navigationItem.title = "The title"; //In swift 4
or
you can just
self.title = "The title"
will do the trick
you can also put the above statement in the app delegate to replicate globally.
This code did not work for me
self.navigationItem.title = #"Title here";
But this worked.
self.title = "Title Name"
Use it in 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";
very important note : make sure you make the changes that mentioned above in parent view Controller's viewDidLoad method, or parents navigation bar in storyboard
Sometimes you might get a case where you are updating the title and it is not causing any effect, this can be when you have a custom view on the topItem therefore you might want to update it like this: (self.navigationController?.navigationBar.topItem?.titleView as? UILabel)?.text = "My Happy New Title"
Related
I have dragged a navigation bar to the top of my UITableViewController in the storyboard. In viewDidLoad I want to update the title in the navigation controller but [self setTitle=#"New Title"]; is not working.
You can try this:
IBOutlet UINavigationItem* barItem = /*(the bar your want to change in storyboard)*/;
barItem.title = #"new title";
If you want to change it, use barItem.title = ... or self.navigationItem.title = ...
You must ensure that the view controller (self) inherits from UINavigationController.
Trying to set a NavBar programaticaly, and my title isn't showing in the navbar. Any ideas as to whats happening?
UINavigationBar *navbar = [[UINavigationBar alloc]initWithFrame:CGRectMake(0, 0, 320, 50)];
navbar.topItem.title = #"My accounts";
[self.view addSubview:navbar];
The problem here is your use of the UINavigationBar.
Read the documentation for UINavigationController programming here.
You should not be creating your own UINavigationBar and adding it as a subview to your view controller's view.
The correct thing to do is to create a UINavigationController and add your view controller to its stack.
So something like this:
CustomViewController *myViewController = ...// whatever initialization you do
UINavigationController *navController = [[UINavigationController alloc] initWithRootViewController:myViewController];
Each view controller has its own UINavigationItem that the navigation controller uses to set up the navigation bar's view when that view controller is at the top of the stack.
So in your CustomViewController.m, possibly in the init method or in viewDidLoad,
you can set up the navigation item by doing something like this:
self.navigationItem.title = #"whatever";
self.navigationItem.rightBarButtonItem = ...// create a UIBarButtonItem and set it here if you want a button on the right side of the navigation bar
So once again, you do not touch the navigation bar directly or add it to your view hierarchy. The UINavigationController handles all of that for you.
Use
self.navigationItem.title = #"the title";
as setting navBar.topItem.title will not work in all circumstances.
I use a UINavigationController in my app and am having an issue with the back button titles. I have the title programmatically set for any page that I push to. On some of the pages the back button will correctly display the previous pages title, but then on select other pages it just says 'back'.
I have a basic implementation on pushing to a new VC:
- (void)pushToNewVC
{
NewVC *newVC = [[NewVC alloc] init];
[self.navigationController pushViewController:newVC animated:YES];
}
In the viewDidLoad of newVC/all of myVC's I have self.title = #"Title".
The problem is that in some cases it works, but then in others in will just say 'Back' rather than the previous pages title.
I've noticed a similar behavior that if the back button's title length won't fit in the Navigation Bar with the title of the pushed VC, the Back Button's title will fallback to just < Back.
On the VCs where the inconsistent behavior is happening, try setting the title of pushing VC to a shorter word just to test and confirm if that is indeed what's happening. Hope this helps.
Setting self.title = #"Title" will just set the title og the UINavigationBar, not the back button. Use the following code to set the back button title:
UIBarButtonItem *backButton = [[UIBarButtonItem alloc]
initWithTitle: #"Back Button Text"
style: UIBarButtonItemStyleBordered
target: nil action: nil];
[self.navigationItem setBackBarButtonItem: backButton];
I am trying to add a bar button to my iOS app and can't get it to show up. I can see the bar in the Navigation Item's view hierarchy by setting a breakpoint. If it helps, I chose 'Embed Navigation Controller'. Any idea what's going on?
UIBarButtonItem *item = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAction target:self action:#selector(choosePreferredTerm:)];
[self.navItem setRightBarButtonItem:item animated:YES];
Here is the connection in IB
This is what the embedded Navigation Controller looks like:
This is what it looks like on the sim:
Try this rather than using custom outlet "navItem" also insure your application has a navigation controller in place
self.navigationItem.rightBarButtonItem = barButton;
I want to make a simple view with a navigation bar and a table view. So I created a subclass of UINavigationController and I added a UITableView in it. My is is also the UITableViewDataSource and UITableViewDelegate. Now I want to add UIBarButton in the navigation bar but it doesn't work :
UIBarButtonItem *anotherButton = [[UIBarButtonItem alloc] initWithTitle:[[Translator instance] getTranslation:#"Back"]
style:UIBarButtonItemStylePlain
target:self.parentViewController
action:#selector(dismissFiltersModal:)];
self.navigationItem.backBarButtonItem = anotherButton;
There's no error the view is displayed but not the button. How can I do to display it?
jafar,
as Apple doc suggested you should avoid to subclass UINavigationController.
This class is not intended for subclassing.
Said this, you could follow these simply steps (some advice for you):
Create your controller (say MyController) that extends UIViewController and add the table here. Set your controller as the delegate and data source for that table. If you have created by xib you need an outlet for your table view.
[P.S. you could also subclass a UITableViewController, it has a table view its own]
In viewDidLoad of MyController customize the navigation bar.
For example
UIBarButtonItem* myButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self action:#selector(someSelector)];
self.navigationItem.leftBarButtonItem = myButton;
You could also customize the title.
For example
self.title = #"Your custom title";
Somewhere in your code create a UINavigationController and add as a root view controller an instance of MyController.
For example:
MyController* myCtr = // alloc-init here
UINavigationController* nc = [[UINavigationController alloc] initWithRootViewController:myCtr];
The backBarButtonItem should be set on the previous view controller, not the current one to be displayed. For example, if your you are on view A, navigate to B from it, inside B A's backBarButtonItem will be displayed, not B's.
If you just want to display an item on the left hand side of the navigation bar, you can use leftBarButtonItem instead, which will display a normal bar button on that controller. It will be rectangular, not an arrow like normal back buttons though.
How about you try this :
UIButton* backButton = [UIButton buttonWithType:101]; // left-pointing shape!
[backButton addTarget:self action:#selector(dismissFiltersModal:) forControlEvents:UIControlEventTouchUpInside];
[backButton setTitle:#"Back" forState:UIControlStateNormal];
// create button item --
UIBarButtonItem* backItem = [[UIBarButtonItem alloc] initWithCustomView:backButton];
// add to toolbar
self.navigationItem.leftBarButtonItem = backItem;