Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

enter image description here

trying to change the bartint color with

[_searchController.searchBar setBarTintColor:[UIColor clearColor]];

but I am getting this enter image description here

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

enter image description here

like image 403
Nick Ginanto Avatar asked Mar 02 '15 09:03

Nick Ginanto


2 Answers

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 :

enter image description here

like image 50
Dharmesh Dhorajiya Avatar answered Oct 11 '22 12:10

Dharmesh Dhorajiya


For Swift Users , just add these two lines :-

searchBar.barTintColor = UIColor.clearColor()
searchBar.backgroundImage = UIImage()
self.navigationItem.titleView = searchBar
like image 20
Chathuranga Silva Avatar answered Oct 11 '22 12:10

Chathuranga Silva