Hey all. I'm still pretty new to iPhone development, and I'm having a bit of trouble figuring out how to change the title of my Navigation Bar. On another question on this site somebody recommended using :
viewController.title = #"title text";
but that isn't working for me...Do I need to add a UINavigationController to accomplish this? Or maybe just an outlet from my UIViewController subclass? If it helps, I defined the navigation bar in IB and I'm trying to set its title in my UIViewController subclass. This is another one of those simple things that gives me a headache. Putting self.title = #"title text"; in viewDidLoad and initWithNibName didn't work either. Anybody know what's happening and how to get it happening right?
Thanks!
The view controller must be a child of some UINavigationController for the .title property to take effect. If the UINavigationBar is simply a view, you need to push a navigation item containing the title, or modify the last navigation item:
UINavigationItem* item = [[UINavigationItem alloc] initWithTitle:#"title text"];
...
[bar pushNavigationItem:item animated:YES];
[item release];
or
bar.topItem.title = #"title text";
if you are doing it all by code in the viewDidLoad method of the UIViewController you should only add self.title = #"title text";
something like this:
- (void)viewDidLoad {
[super viewDidLoad];
self.title = #"title";
}
you could also try self.navigationItem.title = #"title";
also check if your navigationItem is not null and if you have set a custom background to the navigationbar check if the title is set without it.
There's one issue with using self.title = #"title";
If you're using Navigation Bar along with Tab bar, the above line also changes the label for the Tab Bar Item. To avoid this, use what #testing suggested
self.navigationItem.title = #"MyTitle";
If you want to change navbar title (not navbar back button title!) this code will be work.
self.navigationController.topViewController.title = #"info";
If you want to change the title of a navBar inside a tabBar controller, do this:
-(void)viewDidAppear:(BOOL)animated {
self.navigationController.navigationBar.topItem.title = #"myTitle";
}
In my navigation based app I do this:
myViewController.navigationItem.title = #"MyTitle";
I had a navigation controllers integrated in a TabbarController. This worked
self.navigationItem.title=#"title";
By default the navigation controller displays the title of the 'topitem'
so in your viewdidload method of your appdelegate you can. I tested it and it works
navController.navigationBar.topItem.title = #"Test";
UINavigationItem* item = [[UINavigationItem alloc] initWithTitle:#"title text"];
...
[bar pushNavigationItem:item animated:YES];
[item release];
This code worked.
If you are working with Storyboards, you can click on the controller, switch to the properties tab, and set the title text there.
I guess you need a dynamic title that is why you don't set it in IB.
And I presume your viewController object is the one specified in the NIB?
Perhaps trying setting it to a dummy value in IB and then debug the methods to see which controller has the dummy value - assuming it appears as the title...
From within your TableViewController.m
:
self.navigationController.navigationBar.topItem.title = #"Blah blah Some Amazing title";
For all your Swift-ers out there, this worked perfectly for me. It's notably one of the shorter ways to accomplish setting the title, as well:
override public func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
if segue.identifier == "presentLineItem" {
print("Setting Title")
var vc = segue.destinationViewController as! LineItemsTableViewController
vc.navigationItem.title = "Line Item"
}
}
Related
I'm surprised that not "more" has been asked about this :-)
My app is a standard tab bar controller with multiple sections. Each section (tab) contains a navigation controller that controls a couple of view controllers (your basic tableview + detailview type of setup). Now that I've added a 6th tab, iOS creates the default "More" tab that controls the final two tabs. I need to eliminate the "More" text in the middle of the navigation bar (Note: Not the button text, nor the title of the tab itself), and apply a custom background image to the navigation bar.
Note: This question is about customizing the "More" navigation bar - I've successfully modified the background image and titleView text on all of the non-iOS created navigation bars.
In my app delegate, the app is put together thusly:
Create View Controllers:
ViewController1 *vc1 = [ViewController1 alloc] initWithNibName#"View1" bundle:nil];
vc1.title = #"VC1";
vc1.tabBarImage.image = [UIImage imageNamed:#"image1.png"];
Repeat the above routine 5 more times for view controllers vc2 through vc6.
Create the individual navigation controllers:
UINavigationController *nc1 = [UINavigationController alloc] initWithRootViewController:vc1];
Repeat 5 more times for nav controllers nc2 - nc6.
Add Nav Controllers to Tab Bar Controller
self.tabBarController.viewControllers = [NSArray arrayWithObjects: vc1, vc2, vc3, vc4, vc5, vc6, nil];
All of the code above works perfectly. No issues.
I then add a custom background image to the More navigation controller thusly:
if (self.tabBarController.moreNavigationController){
if ([self.tabBarController.moreNavigationController.navigationBar respondsToSelector:#selector(setBackgroundImage:forBarMetrics:)] ) {
UIImage *image = [UIImage imageNamed:#"navlogo.png"];
[self.tabBarController.moreNavigationController.navigationBar setBackgroundImage:image forBarMetrics:UIBarMetricsDefault];
} else {
UINavigationBar *navbar = self.tabBarController.moreNavigationController.navigationBar;
UIImage *headerImg = [UIImage imageNamed:#"navlogo.png"];
[navbar setBackgroundImage:headerImg forBarMetrics:UIBarMetricsDefault];
}
}
This too works just fine. No issues
Since my custom background contains the client's logo dead center, I need to remove the text of the titleView that by default reads "More". Note, I'm not talking about the text of the Navigation BUTTON, but the label in the middle of the navigation bar.
Logically, one would assume that this would work:
UILabel *label = [[UILabel alloc] init];
label.text = #"";
self.tabBarController.moreNavigationController.navigationItem.titleView = label;
...because I do this in all of individual view controllers, substituting self.navigationItem.titleView for self.tabBarController.moreNavigationController, etc.
But this doesn't work! I can successfully change both the background image and the titleView text on all of my navigation controllers with this exact same code (again, substituting self.navigationController.navigationItem for the moreNavController stuff...). However, in the app delegate, I can only set the background image, but not the titleView of the More nav controller.
Any solutions would be greatly appreciated.
VB
Turns out, my original code was correct, just in the wrong place.
UILabel *label = [[UILabel alloc] init];
label.text = #"";
self.tabBarController.moreNavigationController.navigationBar.topItem.titleView = label;
However, I was previously executing this before I added the tabBarController as my rootViewController
The correct order is:
self.window.rootViewController = self.tabBarController;
UILabel *label = [[UILabel alloc] init];
label.text = #"";
self.tabBarController.moreNavigationController.navigationBar.topItem.titleView = label;
Posting Swift5 version as it took me quite some time to make it work properly
moreNavigationController.viewControllers[0].tabBarItem = UITabBarItem(title: "My Title", image: UIImage(named: "MoreTab")!, tag: 1111)
moreNavigationController.navigationBar.topItem!.title = "My Title"
Please note that changing moreNavigationController.tabBarItem doesn't work quite well. I do this in viewDidLoad() of a class which extends UITabBarController
E.g., in application:didFinishLaunchingWithOptions: of the Tab Controller iOS Template
// Override point for customization after application launch.
UITabBarController *tabBarController = (UITabBarController *)(self.window.rootViewController);
UINavigationController *moreNavigationController = tabBarController.moreNavigationController;
moreNavigationController.navigationBar.topItem.title = #"";
This changes the navigation bar title, but leaves the tab label title as "More".
I found one
Swift 2.2
Put this in didFinishLaunchingWithOptions
tabBarCtr.moreNavigationController.delegate = self
tabBarController.moreNavigationController.viewControllers.first?.title = ""
Implement this delegate method
func navigationController(navigationController: UINavigationController, willShowViewController viewController: UIViewController, animated: Bool)
{
navigationController.navigationBar.topItem?.rightBarButtonItems = nil
navigationController.viewControllers.first?.title = ""
}
This will also remove the edit button on top of more navigation controller navigation bar.
This might not be applicable to everyone on the thread but for me... I had a tabBarController with > 5 items... I needed to set the title on the automatically generated moreNavigationController... it was as simple as specifying self.title=#"title" in the viewDidLoad method of the target detailviewcontroller.
You can change the title inside the viewDidLoad function
self.title = #""
I am currently trying to change the back button text of a subview that is loaded of a tablecell touch. However even with the way I am trying to implement it it still shows the parents title in the back button.
I am trying to load a new value into the back button inside viewdidload method like so
UIBarButtonItem *myBarButtonItem = [[UIBarButtonItem alloc] init];
myBarButtonItem.title = #"Back";
self.navigationItem.backBarButtonItem = myBarButtonItem;
[myBarButtonItem release];
however its not working.
You need to change self.navigationItem.backBarButtonItem from previous view, not current view (I know, it seems to be a little bit illogical). For example, in your table view you can do the following:
- (void)viewDidLoad
{
[super viewDidLoad];
[self setTitle:#"My title"];
UIBarButtonItem *boton = [[UIBarButtonItem alloc] initWithTitle:#"Custom back button text" style:UIBarButtonItemStyleBordered target:self action:#selector(mySelector:)];
self.navigationItem.backBarButtonItem = boton;
[boton release];
}
This is where the documentation is not so clear until you have read and re-read and play around with each settings.
To change the title of the default back button add this line in viewDidLoad()
self.navigationController.navigationBar.topItem.title = #"Your Label";
If you want the button to be invisible - then set the value to #"" empty string.
Okay figured it out, I posted this code in the parent views tableView:didSelectRowAtIndexPath: method. this is how my one looks with multiple tablecells the user can select. Hope this helps.
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
// Navigation logic may go here. Create and push another view controller.
if (indexPath.section == 0) { //--- picks (first) section
ChildViewController *childViewController = [[ChildViewController alloc] initWithNibName:#"ChildViewController" bundle:nil];
// ...
// Pass the selected object to the new view controller.
[self.navigationController pushViewController:childViewController animated:YES];
//--- this sets the back button to "Back" every time you load the child view.
self.navigationItem.backBarButtonItem = [[[UIBarButtonItem alloc] initWithTitle:#"Back" style: UIBarButtonItemStyleBordered target:nil action:nil] autorelease];
if(indexPath.row == 0) { //--- picks row
childViewController.title = #"Bike";
}
if(indexPath.row == 1) {
childViewController.title = #"Model";
}
[vehicleSearchResponseTableViewController release];
}
}
The best way with Xcode 5 to change the back button name is to edit the Back Button field in IB of the Navigation Item on the View Controller to which the back button will return. For example, if you have a list TableViewController that goes to a detail screen, change the Back Button on the list TableViewController's Navigation item (in IB) to change the back button name that the detail screen displays.
It's not gonna work the way you're trying to do. Navigation button will always have title of previous view. What you can do though - change title of first view before pushing the new one. This is the only was I could find to solve same problem.
Your code looks fine so far.
Is this code executed before the
[super viewDidLoad];
statement (wrong) or after it (good)?
After viewDidLoad
self.navigationController!.navigationBar.backItem?.title = "Back"
My suggestion was to add a separate Label to parents Page title Bar.
Worked fine for me.
let titleLabel = UILabel(frame: CGRect(x: (view.frame.width * (3/8)), y: 0, width: (view.frame.width * 0.25 ), height:30))
titleLabel.text = "Title"
titleLabel.textAlignment = .center
titleLabel.textColor = UIColor.white
titleLabel.font = UIFont.systemFont(ofSize: 20)
navigationItem.titleView = titleLabel
This article should do what you want.
From the author:
As you can see above, all you need to do is set the leftBarButtonItem of the controller and it will hide the back button. The selector handleBack now handles the back event and you need to then manually pop the view controller off the UINavigationController’s stack. You can implement any of your own code in there, even blocking leaving the page.
A good way to do this is to set the title of the back button in the parent view controller before calling the child like this:
[self.navigationItem.backBarButtonItem setTitle:#"Your Custom Title"];
This title will be placed in the back button on the child view. The important point to get here is this title is used in the child view as the back button to the parent view.
I am trying the following code:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
Thing *sub = [[subscriptions objectAtIndex:indexPath.row] retain];
StoriesViewController *thing = [[StoriesViewController alloc] initWithThing:sub];
thing.navigationController.title = sub.title;
[self.navigationController pushViewController:thing animated:YES];
[thing release];
[sub release];
[tableView deselectRowAtIndexPath:indexPath animated:YES];
}
I thought this is how you correctly set the title for pushing the controller. I tried thing.title however that was setting the TabBarItem's title instead.
thing.navigationItem.title = sub.title;
or in StoriesViewController.m file in viewDidLoad method:
self.navigationItem.title = sub.title
It's possible you're pushing a UITabBarController instead of your regular view controller. In this case, the navigationItem will display the title of the current controller, the tab bar controller, even though you see another view. You would have to change the tab bar controller's title to change the text displayed above.
self.tabBarController.title = #"Your title";
The title needs to be set in (or after) viewDidLoad, so if you want to set from another class that is creating StoriesViewController.
Make another property and have that class set it.
In StoriesViewController's viewDidLoad, set the title from that property.
This is because outlets aren't initialized to their view counterparts until viewDidLoad.
It looks like StoriesViewController is holding on to sub (does the initWithThing set a property to it?) If so, just set the title in viewDidLoad to the Thing property's title.
[thing setTitle: sub.title];
The solution was thing.navigationItem.title = #"title"; however it turns out that subclassing a UINavigationController broke it somehow and my solution to that was to use a category rather than a subclass
If your UIViewController is part of a UITabController, then you can do:
self.tabBarController.title = sub.title;
I've used [[self navigationController] setTitle:#"Test Title"] to no avail. This is the same way I do it in the rest of my app. What could cause this?
Try setting the title of the navigation item.
self.navigationItem.title = #"Test Title";
Or like this if you prefer
[[self navigationItem] setTitle:#"Test Title"];
You are using the title property incorrectly, the navigationController has a title property because it inherets from UIViewController, the title property is used by NavigationControllers to display a title, so if you wanted the title you gave your NavigationController to show you would need to present it in another NavigationCOntroller...But what you need to do, is set the viewControllers that you are displaying titles instead of the NavigationController, now whenever u display that VC youll see the title in the navigation bar...
In short...the viewcontrollers title property is used by its navigationController to display the title on the navigation bar when that viewcontroller is on the top of the navigation stack...
CustomViewController *viewController = [[CustomViewController alloc] init];
[viewController setTitle:#"CustomViewController!";
[custonNavigationController pushViewController:viewController animated:YES];
This is a simplification of what Daniel had said. This method properly follows stack protocol.
SecondViewController *s1=[[SecondViewController alloc]initWithNibName:#"SecondViewController" bundle:nil];
[s1 setTitle:#"Second Page"];
[self pushViewController:s1 animated:YES];
Worked for me.
I have a UITabBar and a UINavigationBar created in IB, when I launch my application everytime I navigate the UINavigation title change because I'm using
self.title = #"NAME";
my problem is that the UITabBarItem will change with the same name at the same time.
I want to put a static name only for the UITabBarItem, how to do it in IB or programatically maybe.
Thanks,
try returning an explicit tabBarItem for your controller, and set its title yourself.
- (UITabBarItem *) tabBarItem {
return [[[UITabBarItem alloc] initWithTitle: ...
I find the solution in this adress Stackoverflow navigationItem Title Sorry I didn't find it before posting my question.
try this, in your viewDidLoad
self.tabBarController.navigationItem.title = #"title";