searchbar changes color when in navigation bar - ios

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

Related

UISearchBar is not extending its background color under the UIStatusBar in ios 8

I have an UIViewController with a UICollectionView in it, and a UISearchBar on top of it. The problem is that when I tap on my search bar, and it hides the navigation bar, search bar background color is not extending under the status bar.
Here is how I set up the UISearchBar and UISearchController:
UINavigationController *searchResultsController = [[self storyboard] instantiateViewControllerWithIdentifier:[ADC_SEARCH_RESULTS_CONTROLLER_IDENT copy]];
self.searchController = [[UISearchController alloc] initWithSearchResultsController:searchResultsController];
self.searchController.searchResultsUpdater = self;
self.searchController.searchBar.frame = CGRectMake(0, 0, self.view.frame.size.width, 44.0);
[self.view insertSubview:self.searchController.searchBar belowSubview:self.navigationController.navigationBar];
self.definesPresentationContext = YES;
if ([self respondsToSelector:#selector(edgesForExtendedLayout)])
self.edgesForExtendedLayout = UIRectEdgeNone;
SuggestionResultsTVC *vc = (SuggestionResultsTVC *)searchResultsController.topViewController;
vc.tableView.contentInset = UIEdgeInsetsMake(0.0f, 0.f, 0.f, 0.f);
self.searchController.searchBar.clipsToBounds = YES;
Here is a screen shot of the UIViewController when search bar is dismissed:
http://s4.postimg.org/x0rncg5e5/Screen_Shot_2015_09_02_at_5_09_41_PM.png
And here is a screen shot of the view controller when search bar is active:
http://s4.postimg.org/tv71m8mrx/Screen_Shot_2015_09_02_at_5_10_04_PM.png
In your storyboard or xib file, go to Attributes Inspector -> Simulated metrics and change status bar from "inferred" to "none".
I build the app in simulator, and activated the UISearchBar, then I enter the "View UI Hierarchy" option in xcode, here is a screen where you can find it:
http://s16.postimg.org/wyb8jxqw5/Screen_Shot_2015_09_02_at_5_47_58_PM.png
And there it was clear for me that the gray background color comes from a UIDimmingView under my UIViewController -> the solution was to change the color of the UIView of my UIViewController, that is above that UIDimmingView to the color matching UISearchBar background.
It looks to me like your UISearchBar may send a call to its parent's UINavigationController after being tapped. So try tinkering with this:
self.navigationController.view.backgroundColor = [UIColor blackColor];
self.navigationController.navigationBar.translucent = NO;

How to change background colour behind search bar

I trying to customize my UISearchBar in iOS7/8 and I've met next problem:
the background behind my searchDisplayController is always light grey. This is how it looks like:
And this is my code of some search bar customization:
self.searchDisplayController.searchResultsTableView.backgroundColor = UIColorFromRGB(0x171717);
self.searchDisplayController.searchContentsController.view.backgroundColor = UIColorFromRGB(0x171717);
self.searchDisplayController.searchResultsTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
self.searchDisplayController.searchResultsTableView.tableFooterView = [[UIView alloc] initWithFrame:CGRectZero];
Is there any way to fix it? Thanks.
As it turned out this gray/white background is part of refreshControl. So all what you have to do is init it and set the needed background color:
self.refreshControl = [[UIRefreshControl alloc] init];
self.refreshControl.backgroundColor = UIColorFromRGB(0x232323);
You can change it programmatically using this:
[searchBar setTintColor:[UIColor darkGrayColor]];
Or try from Storyboard by changing Bar Tint:
Try by changing the tint color of the searchable in xib
or in code as [self.searchDisplayController setTintColor:[UIColor whiteColor]];searchDisplayController

Setting the tint color when using form sheet modal presentation does not work on iPad

I have a UISearchBar in a modal UIViewController that has presentationStyle set to UIModalPresentationStyleFormSheet.
Setting the tint colour on the search bar is only obeyed on the iphone. The iPad still ends up using the app tint color. Any reason why this is happening?
_searchBar = [[UISearchBar alloc] init];
_searchBar.tintColor = [UIColor whiteColor];
why dont you try appearance for search bar in applicationlaunch.
[[UISearchBar appearance] setTintColor:[UIColor whiteColor]];
That didn't work for me. What worked was setting the UITextField appearance, but that's iOS7 only. You can also loop over the bar's subviews and change the UITextField's tintColor:
[[UITextField appearance] setTintColor:[UIColor blackColor]];

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.

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