Nick Ginanto
Nick Ginanto

Reputation: 32130

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

Upvotes: 5

Views: 2188

Answers (2)

Mudith Chathuranga Silva
Mudith Chathuranga Silva

Reputation: 7434

For Swift Users , just add these two lines :-

searchBar.barTintColor = UIColor.clearColor()
searchBar.backgroundImage = UIImage()
self.navigationItem.titleView = searchBar

Upvotes: 4

Dharmesh Dhorajiya
Dharmesh Dhorajiya

Reputation: 3984

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

Upvotes: 14

Related Questions