UINavigationBar - Set title programmatically? - ios

I have a tab bar application with a different view on each tab. Each view has a UINavigationBar with title set on Interface Builder. I want to change the title based on a clause in the ViewDidLoad method, so if x { change the title }.
I have tried self.title = #"title", but this changes the title of the tab bar item itself.
So, how is this done?

I've set the title programmatically using code something like this:
navBar.topItem.title = #"title";
where navBar is declared as an IBOutlet UINavigationBar linked to the navigation bar in interface builder. This worked in my app; however, I was not using a tab bar.
If navBar.topItem is the tab bar item, I don't see a way for you to change the title that appears on the navigation bar without also changing the title on the tab bar item, since the navBar's topItem and the tab bar item is the same object.

Use
self.navigationItem.title = #"the title";
as setting navBar.topItem.title will not work in all circumstances.

Use this :
CGRect navBarFrame = CGRectMake(0, 0, self.tableView.frame.size.width, 44.0);
UINavigationBar *navBar = [[UINavigationBar alloc] initWithFrame:navBarFrame];
UINavigationItem *navItem = [UINavigationItem alloc];
navItem.title = #"Your Title";
[navBar pushNavigationItem:navItem animated:false];
[self.view addSubView:navBar];
or
self.tableView.tableHeaderView = navBar; etc

If the class is a type of UIViewController, then you can set title as given in the viewDidLoad method.
[self setTitle:#"My Title"];

Create IBOutlet of UINavigationBar
navigationBar.topItem.title = #"Title";
Hope this helps.

In ViewDidLoad of your ViewController.m just write,
self.navigationItem.title=#"Hello World";
Here is the Output:

I used the following:
self.navigationController.navigationBar.topItem.title = #"Title";
However, I also had to call it in a dispatch block as it wouldn't change the title when called within the viewDidLoad w/o it.

Note that in order for the title to show up AT ALL the Navigation Controller's delegate MUST be set to the navigation bar itself or the title will NEVER show!

I achieved this way in Swift 5
override func viewDidLoad() {
super.viewDidLoad()
navigationItem.title = "Title"
}

You can also just set the title in the ViewDidLoad under your ViewController or TableViewController using
_title = #"Title";
or
self.title = #"Title";

Try this,
self.navigationItem.title = #"Your title";

Related

how to add a UISearchBar in the middle of a NavigationBar [duplicate]

How can I show a UISearchBar in the NavigationBar?
I can't figure out how to do this.
Your help is very much appreciated.
To put searchBar into the center of navigationBar:
self.navigationItem.titleView = self.searchBarTop;
To put searchBar to the left/right side of navigationBar:
UIBarButtonItem *searchBarItem = [[UIBarButtonItem alloc] initWithCustomView:searchBar];
self.navigationItem.rightBarButtonItem = searchBarItem;
As of iOS 7, the UISearchDisplayController supports this by default. Set the UISearchDisplayController's displaysSearchBarInNavigationBar = YES to get this working easily.
Per the documentation:
Starting in iOS 7.0, you can use a search display controller with a navigation bar (an instance of the UINavigationBar class) by configuring the search display controller’s displaysSearchBarInNavigationBar and navigationItem properties.
As one commenter noted, using searchDisplayController.displaysSearchBarInNavigationBar = true ends up hiding any existing left/right bar button items.
I've found two different ways of adding a searchBar to a navigationBar using iOS7's new property on searchDisplayController.
1) Nib Based Approach
If you're using a .xib, you can set a User Defined Runtime Attribute for this value and for whatever reason, the leftBarButtonItem stays in tact. I have not tested it with a rightBarButtonItem.
2) Code (Timing Matters)
If you want to implement in code, timing seems to matter. It seems that you must add the searchBar to the navigationBar first, then set your barButtonItem.
- (void)viewDidLoad
{
...
self.searchDisplayController.displaysSearchBarInNavigationBar = true;
self.navigationItem.leftBarButtonItem = [UIBarButtonItem new];
...
}
Check out Apple's UICatalog sample code. It shows how to use the new UISearchController in three different ways: modally, in nav bar, and below the navigation bar.
Objective C code snippet for UISearchBar in NavigationBar
- (void)viewDidLoad {
UISearchController *searchController = [[UISearchController alloc] initWithSearchResultsController:nil];
if (#available(iOS 11.0, *)) {
self.navigationItem.searchController = searchController;
} else {
self.navigationItem.titleView = searchController.searchBar;
}
}
Read more here

How to add Text Field and Images in Navigation Bar of Tab Bar?

I want to add text field and search icon on navigation bar of tab-bar controller. Please check attach image.
Note: This is home screen and if user click on any menu item it will goes to detailvc so i add one navigation controller on homevc and hide it.
I want to show search icon and text-field at navigation-bar of tabbar. how can i do this ?
set textfield with left view in navigation title view
navigationItem.titleView = UITextField()
Subclass the navigationbar
#interface SearchNavigationBar : UINavigationBar
-(void)layoutSubviews
{
[super layoutSubviews];
UIBarButtonItem *buttonItem = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:#"searchImage"] style:UIBarButtonItemStylePlain target:self action:#selector(searchPrompt:)];
self.topItem.rightBarButtonItem = buttonItem;
}
Add it as the class of your navigation controller
and get the object this way
UINavigationController *navBar = [[UINavigationController alloc] initWithNavigationBarClass:[SwitchAssessmentNavigationBar class] toolbarClass:nil];
and then you can call custom methods on the tool bar such as delegates that trigger events in your main page to change
searchBar in center of navigationBar:
self.navigationItem.titleView = self.searchBarTop;
searchBar in left/right side of navigationBar:
UIBarButtonItem *searchBarItem = [[UIBarButtonItem alloc] initWithCustomView:searchBar];
self.navigationItem.rightBarButtonItem = searchBarItem;
Create an UIView custom class which will hold your custom search bar.
Then add that view's instance into your navigationController's view.
[self.navigationController.view addSubview:yourCustomView];
I have to add on own custom custom class of UItabbar-Contrller

UINavigation title not showing?

This is in my view did load. There is no title showing at all? I have a UITabBar with a UITableView and UInavigationBar inside the UITabBar.
//Creates navigation bar.
UINavigationBar *myNavBar = [[UINavigationBar alloc] initWithFrame:CGRectMake(0, 0, 320, 40)];
myNavBar.barStyle = UIBarStyleBlack;
myNavBar.topItem.title = #"My Title";
[self.view addSubview:myNavBar];
myNavBar.topItem.title = #"My Title";
doesn't work because topItem is nil since there is no item. You need to add UINavigationItems.
self.title = #"Some Title";
doesn't work because UINavigationController picks up UIViewController's title and there is no UINavigationController here.
self.navigationItem.title=#"My Title"";
doesn't work because navigationItem is nil.
Solution:
Add an item (and set the title in the UINavigationItem) using either -[UINavigationBar pushNavigationItem:animated:] or -[UINavigationBar setItems:]
Use this
self.navigationItem.title=#"My Title"";
It may help you.

UISearchBar in navigationbar

How can I show a UISearchBar in the NavigationBar?
I can't figure out how to do this.
Your help is very much appreciated.
To put searchBar into the center of navigationBar:
self.navigationItem.titleView = self.searchBarTop;
To put searchBar to the left/right side of navigationBar:
UIBarButtonItem *searchBarItem = [[UIBarButtonItem alloc] initWithCustomView:searchBar];
self.navigationItem.rightBarButtonItem = searchBarItem;
As of iOS 7, the UISearchDisplayController supports this by default. Set the UISearchDisplayController's displaysSearchBarInNavigationBar = YES to get this working easily.
Per the documentation:
Starting in iOS 7.0, you can use a search display controller with a navigation bar (an instance of the UINavigationBar class) by configuring the search display controller’s displaysSearchBarInNavigationBar and navigationItem properties.
As one commenter noted, using searchDisplayController.displaysSearchBarInNavigationBar = true ends up hiding any existing left/right bar button items.
I've found two different ways of adding a searchBar to a navigationBar using iOS7's new property on searchDisplayController.
1) Nib Based Approach
If you're using a .xib, you can set a User Defined Runtime Attribute for this value and for whatever reason, the leftBarButtonItem stays in tact. I have not tested it with a rightBarButtonItem.
2) Code (Timing Matters)
If you want to implement in code, timing seems to matter. It seems that you must add the searchBar to the navigationBar first, then set your barButtonItem.
- (void)viewDidLoad
{
...
self.searchDisplayController.displaysSearchBarInNavigationBar = true;
self.navigationItem.leftBarButtonItem = [UIBarButtonItem new];
...
}
Check out Apple's UICatalog sample code. It shows how to use the new UISearchController in three different ways: modally, in nav bar, and below the navigation bar.
Objective C code snippet for UISearchBar in NavigationBar
- (void)viewDidLoad {
UISearchController *searchController = [[UISearchController alloc] initWithSearchResultsController:nil];
if (#available(iOS 11.0, *)) {
self.navigationItem.searchController = searchController;
} else {
self.navigationItem.titleView = searchController.searchBar;
}
}
Read more here

Hide the title from UINavigationBar?

Is there any way to hide the title view in a UINavigationBar?
self.navigationItem.titleView = [[UIView alloc] initWithFrame:CGRectZero];
self.title = #"Home";
Setting to nil will NOT work as described in documentation:
If this property value is nil, the navigation item’s title is displayed in the center of the navigation bar when the receiver is the top item. If you set this property to a custom title, it is displayed instead of the title.
The titleView is a UIView :
titleView
A custom view displayed in the center of the navigation bar when this
item is the top item.
#property(nonatomic, retain) UIView *titleView
So I think you can try this :
[titleView setHidden:YES];
Another option which preserves the back button (as answered here: https://stackoverflow.com/a/23113326/1156575):
[self.navigationController.navigationBar setTitleTextAttributes:#{
NSForegroundColorAttributeName : [UIColor clearColor]
}];
For me the solution in swift needed to be in my navigationbar subclass:
self.titleTextAttributes = [NSForegroundColorAttributeName: UIColor.clearColor()]
With the clear color the title disappears. As Josema described, you can also do this by accessing the navigationbar from your navigationcontroller:
self.navigationController!.navigationBar.titleTextAttributes = [NSForegroundColorAttributeName: UIColor.clearColor()]
This is what worked for me:
self.navigationController?.navigationBar.titleTextAttributes = [NSAttributedStringKey.foregroundColor: UIColor.clear]

Resources