75

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.

dev.nikolaz
  • 3,184
  • 3
  • 19
  • 32

5 Answers5

189

Need to use:

searchBar.barTintColor = [UIColor redColor];

enter image description here

All thanks!

dev.nikolaz
  • 3,184
  • 3
  • 19
  • 32
  • 3
    Is that a way to remove the transparency in the searchbar ? because the color is not really red ! – Aladdin Gallas Nov 15 '13 at 19:12
  • 5
    @AladdinGallas you simply set UISearchBar's translucent property to NO like so: `[searchbar setTranslucent:NO];` – Pavan Dec 30 '13 at 17:59
  • i want to add rounded corners to this bar? how can i do that? – Anish Apr 02 '14 at 09:17
  • 1
    this does give the bar a red tone, but there is still transparency/alpha to this tone. is there a way to get rid of that and get a pure color as the tint? – Julian B. Sep 29 '14 at 17:38
  • 4
    Set the searchbar style to UISearchBarStyleMinimal, than you get the pure color. – Jeroen de Leeuw Oct 10 '14 at 15:15
  • I tried this but it didn't change the color for some reason. It only changed the color of the cursor? Anyone have any clues on why this happeneed? –  Nov 17 '16 at 20:54
  • @1290 please check if you set `searchBarStyle = .minimal`, change it to default – Milan Kamilya Jun 12 '17 at 15:53
59

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];
Vadoff
  • 9,219
  • 6
  • 43
  • 39
33

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.

zeeawan
  • 6,667
  • 2
  • 50
  • 56
6

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;

            }
chings228
  • 1,859
  • 24
  • 24
4

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!

FabKremer
  • 2,149
  • 2
  • 17
  • 29