UINavigationBar not displaying title in iOS - ios

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.

Related

How to set NavigationController navigationItem title from ViewController loaded by Xib

I've a UIViewController which loads it's view form a Xib file. In my AppDelegate.m I would like to initiate my rootViewController with that myViewController and I would like that the title of the navigtionItem is set by taking the NavigationBar view which is part of the Xib file of the myViewController:
MyViewController *myViewController = [[MyViewController alloc] init];
UINavigationController *navigationController = [[UINavigationController alloc] initWithRootViewController:myViewController];
According to the documentation
Each time the top-level view controller changes, the navigation controller updates the navigation bar accordingly.
In my setup this seems not to happen or I haven't indicated that my NavigationBar view is actually the navigationBar to consider.
How can I tell the NavigationController to update it's navigationItem content (title, left and right bar buttons) according the NavigationBar view which is part of the Xib file from myViewController?
Update
My Xib layout looks like this:
I know I can set those items in code by:
self.navigationItem.titleView = ...;
self.navigationItem.leftBarButtonItem = ...;
But I want them do design in the Xib file of the ViewController and they should be used in the NavigationController.
In ViewWillAppear method in your ViewController set-
self.navigationItem.title = #"Title name";
Also in the same way you can set self.navigationItem.leftBarButton and RightBarButton items
Adding a UINavigationBar view to the main UIView doesn't do what you think it does. It merely adds a navigation bar view there in the view, it does not tie into the UINavigationController.
If you use a storyboard, you can add the UINavigationItem as a child of the UIViewController. But since you are not using a storyboard, the easiest way to do it is to update the navigationItem in viewDidLoad.
Sidenote: Apple does not recommend using viewDidLoad to setup the navigation item. If you really want to do it the proper way, you can for example do it in the navigationItem getter.
- (UINavigationItem*)navigationItem
{
// get super navigation item
UINavigationItem *navItem = [super navigationItem];
// do stuff
return navItem;
}

UINavigationController Title(s) and buttons are not displayed within UITabBarController

Im having difficulty getting the titles of the navigation bars to display along with the buttons within my tabBarController interface. Im creating the tabBarController programmatically. Here is the screenshot for reference.
I have tried putting self.navigationController.navigationBarHidden = YES; within the alloc/init method of the tabBarController which is allocated in the appDelegate and set as the windows rootViewController. I've also tried to set its title with this code self.navigationController.title = [[self.viewControllers objectAtIndex:self.selectedIndex]title ];. I have also tried using the same code within the viewDidLoad method of my tabBarController class. Within the UITabBarController's alloc/init method I do have this code to set the nav controllers that I have added to the viewControllers array.
UINavigationController *nav2 = [[UINavigationController alloc]initWithRootViewController:contactsTblView];
nav2.title = #"Contacts";
nav2.navigationItem.title = #"Contacts";
nav2.tabBarItem = [[UITabBarItem alloc]initWithTabBarSystemItem:UITabBarSystemItemContacts tag:2];
nav2.delegate = self ;
The title that appears in the navigation bar is the title of the currently showing view controller (the top of the navigation controller's stack). You should set the title of the individual view controllers embedded in the navigation controller, not the navigation controller itself.

Edit Components of a Navigation Bar

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.

How to get Navigation bar inside UIPopover Controller

I am using a uipopover controller with navigation bar.Actually i want to have navigation bar inside the popover controller. i tried changing the background color of the navigation controller but it didnt look as i expected.The navigation bar buttons seems to be attached with the popover corners.Please find the screenshot attached.Below is my code which i used to create the popover.
WLViewBookmarkViewController * viewBookmarkViewController = [[WLViewBookmarkViewController alloc] initWithStyle:UITableViewStyleGrouped];
viewBookmarkViewController.delegate = self;
UINavigationController *viewBookmarkNavController = [[UINavigationController alloc] initWithRootViewController:viewBookmarkViewController];
viewBookmarkNavController.navigationBar.backgroundColor = [UIColor colorWithRed:9/255.0 green:135/255.0 blue:46/255.0 alpha:1.0];
self.bookmarkPopoverController = [[UIPopoverController alloc] initWithContentViewController:viewBookmarkNavController];
[self.bookmarkPopoverController presentPopoverFromBarButtonItem:bookmarkButtonItem permittedArrowDirections:UIPopoverArrowDirectionDown|UIPopoverArrowDirectionUp
animated:YES];
Please suggest if there are any other way to achieve this.
I would init the UIPopover controller with the UINavigationController directly
self.bookmarkPopoverController = [[UIPopoverController alloc] initWithContentViewController: viewBookmarkNavController];
Also try settings the content size of the popover by overriding -contentSizeForViewInPopover in the viewBookmarkViewController class to return a CGSize, you should get neater results this way.
I created a Xib in where i had a view for the controller which i had to add inside the popover.i added a table view and a navigation bar into the view.

How to set the title of a Navigation Bar programmatically?

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"

Resources