Hide the title from UINavigationBar? - ios

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]

Related

How to set exact color to uinavigationbar as given, i need below

All i want that the UINavigationBar and the searchView(CustomView)
should have exact same color and without having shadow under the UINavigataionbar. i have tried a lot,i found the soultion for shadow
UINavigationBar.appearance().shadowImage = UIImage()
but still can't able to do same background color on UINavigationBar and searchView. You can see that minor color difference between UINavigationBar and SearchView
Please don't go for guidelines, if u have answer please share.
Note: SearcView is UIView wid label and an image not UISeachBar.
Try setting translucent property of navigationBar to false
Swift
self.navigationController.navigationBar.isTranslucent = false
Objective C
[self.navigationController.navigationBar setTranslucent:NO];
PS: if you want to do it in all the screen, set it globally
Swift
UINavigationBar.appearance().isTranslucent = false
Objective C
[[UINavigationBar appearance] setTranslucent:NO];
Set your UINavigationBar translucent property false.
Storyboard: In Storyboard uncheck translucent property
Swift:
self.navigationController.navigationBar.isTranslucent = false
In AppDelegate (didFinishLaunchingWithOptions):
UINavigationBar.appearance().isTranslucent = false
You can change the color of navigation bar using this
UINavigationBar *bar = [self.navigationController navigationBar];
[bar setTintColor:[UIColor greenColor]]; //Change the color according to your requirement.
Try This One
Objective C
self.navigationController.navigationBarHidden=NO;
[self.navigationController.navigationBar setTitleTextAttributes: #{NSForegroundColorAttributeName:White,NSFontAttributeName:yourFont}];
self.navigationController.navigationBar.barTintColor = yourColor;
Swift
self.navigationController?.navigationBar.barTintColor = .red //yourColor
self.navigationController?.navigationBar.tintColor = .white //yourColor

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

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.

customize UIBarButtonItem in Navigation Bar

I was trying to customizing the UIBarButtonItem in Navigation Bar
I found that there is a property in UIBarButtonItem : "tintColor" , and I set it like this
self.navigationItem.leftBarButtonItem.tintColor = [UIColor redColor];
It works well, but if I try this :
self.navigationItem.leftBarButtonItem.tintColor = [UIColor colorWithPatternImage:[UIImage imageNamed:#"imageName"]];
1. It doesn't work as expect , however , the leftBarButtonItem shows a black appearance ,why ?
If I try a clearColor :
self.navigationItem.leftBarButtonItem.tintColor = [UIColor clearColor];
2. The leftBarButtonItem shows a black appearance still, why ?
3. I want to customize it with a pattern image like above , how can I do that ?
Thank you
You can check this sample code for customizing navigation bar and back button
https://github.com/boctor/idev-recipes/tree/master/CustomBackButton

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