UINavigation title not showing? - ios

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.

Related

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

searchbar changes color when in navigation bar

I added a search bar in the titleview of a navigationitem. Searching works properly, but when using the navigation controller, the background of the searchbar changes colors, as seen below
The only place that I touch color is to change the color of the navigation item in the storyboard via xcode
the relevant code for putting the searchbar in the titleview
_searchController = [[UISearchController alloc] initWithSearchResultsController:nil];
[_searchController.searchBar sizeToFit];
self.navigationItem.titleView = _searchController.searchBar;
What is causing this coloration?
trying to change the bartint color with
[_searchController.searchBar setBarTintColor:[UIColor clearColor]];
but I am getting this
also things that aren't working are
[_searchController.searchBar setBarTintColor:self.navigationController.navigationBar.tintColor];
_searchController.searchBar.backgroundColor = self.navigationController.navigationBar.tintColor;
and with backgroundColor
it seems that this color is somehow other than the settings. See here with redColor
Need to set UISearchBar tint color.
UISearchBar *search=[[UISearchBar alloc]initWithFrame:CGRectMake(10, 20, 100, 20)];
[search setBarTintColor:[UIColor clearColor]];
search.backgroundImage=[UIImage new];
self.navigationItem.titleView=search;
Now navigation bar looks like below image :
For Swift Users , just add these two lines :-
searchBar.barTintColor = UIColor.clearColor()
searchBar.backgroundImage = UIImage()
self.navigationItem.titleView = searchBar

UINavigationController barstyle property changes layout

I have a modal view controller which displays a navigation controller. The navigation controller in-turn has a regular UIViewController as its root view controller. The only UI element that the above-mentioned UIViewController has is a UISwitch.
Now here's the problem: When I change the barStyle property of the navigation controller, the layout of the UISwitch inside the UIViewController changes. Here's what I am talking about:
If I don't set the barStyle property, here's what I get:
http://img535.imageshack.us/img535/2281/plaini.png
The UISwitch is now in its 'exepected' place.
Now if I set the barStyle property,
navController.navigationBar.barStyle = UIBarStyleBlackTranslucent;
Notice that the UISwitch is behind the navigation bar:
http://img853.imageshack.us/img853/2377/blackya.png
Here's the code for the UISwitch in the UIViewController:
- (void)viewDidLoad
{
UISwitch* mySwitch = [[UISwitch alloc] initWithFrame:CGRectMake(20, 20, 100, 100)];
[self.view addSubview:mySwitch];
[mySwitch release];
}
Can someone help me understand what's happening?
The Frame shifts when you use the UIBarStyleBlackTranslucent (which is actually deprecated) property because it assumes you want your view to be underneath
The Apple Documentation says to use the following since UIBarStyleBlackTranslucent is deprecated:
navController.navigationBar.barStyle = UIBarStyleBlack;
navController.navigationBar.translucent = YES;
You could try shifting your view back in the correct place or try using the following:
navController.navigationBar.tintColor = [UIColor blackColor];
navController.navigationBar.translucent = YES;

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]

UINavigationBar - Set title programmatically?

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";

Resources