How to change background color of UISearchBar in iOS7

Objective CIos7UisearchbarBackground ColorUisearchdisplaycontroller

Objective C Problem Overview


How to change background color of UISearchBar in iOS7?

enter image description here

not gray, I want to change color like my uinavigationbar

if I Use this code, that's what comes out

searchBar.backgroundColor = [UIColor redColor];

enter image description here

That is not red color!!! This exact same situation as in background color of navigation bar.

Objective C Solutions


Solution 1 - Objective C

Need to use:

searchBar.barTintColor = [UIColor redColor];

enter image description here

All thanks!

Solution 2 - Objective C

Set the background image to a clear image and you're good to go. This is also pre-ios 7 compatible.

searchBar.backgroundImage = [[UIImage alloc] init]
searchBar.backgroundColor = [UIColor redColor];

Solution 3 - Objective C

If the above solutions don't seem working then make sure that you've set the search bar style to Minimal.

[self.searchDisplayController.searchBar setSearchBarStyle:UISearchBarStyleMinimal];

And for simple searchBar

[self.searchBar setSearchBarStyle:UISearchBarStyleMinimal];

SearchBar Style can also be set from interface builder to Minimal.

Solution 4 - Objective C

it's not really work for me , or sometimes , if you too , try this

for (UIView *view in [[filterTextField.subviews objectAtIndex:0] subviews]){
                if ([NSStringFromClass([view class]) isEqualToString:@"UISearchBarBackground"])
                    view.alpha = 0;
                
            }

Solution 5 - Objective C

If the UISearchBar was defined in the MainStoryBoard, just click on that UISearchBar and take a look to the options you can handle at right. Over there if you click on the fourth tab (the one that looks like a shield) you've got a Bar Tint option. There you can select the UISearchBar color you want.

If not, I guess programatically you can do something like this:

    UISearchBar* sb =[[UISearchBar alloc] init];
    sb.backgroundColor=[UIColor redColor];

I hope this helps!

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
Questiondev.nikolazView Question on Stackoverflow
Solution 1 - Objective Cdev.nikolazView Answer on Stackoverflow
Solution 2 - Objective CVadoffView Answer on Stackoverflow
Solution 3 - Objective CzeeawanView Answer on Stackoverflow
Solution 4 - Objective Cchings228View Answer on Stackoverflow
Solution 5 - Objective CFabKremerView Answer on Stackoverflow