7

I've implemented a Search Bar and I want to change color of Search Bar. How can I do that?

I've tried with:

self.mySearchBar.backgroundColor = [UIColor redColor];

but with no success.

UPDATE (Solved):

I had to remove the default background color before I defined background color with the following code.

for (UIView *subview in mySearchBar.subviews) {
    if ([subview isKindOfClass:NSClassFromString(@"UISearchBarBackground")]) {
        [subview removeFromSuperview];
        break;
    }
} 

... so code would be next:

  for (UIView *subview in mySearchBar.subviews) {
      if ([subview isKindOfClass:NSClassFromString(@"UISearchBarBackground")]) {
          [subview removeFromSuperview];
          break;
      }
  } 

  self.mySearchBar.backgroundColor = [UIColor redColor];
rene
  • 41,474
  • 78
  • 114
  • 152
iWizard
  • 6,816
  • 19
  • 67
  • 103

9 Answers9

29

Try:

self.mySearchBar.barTintColor = [UIColor redColor];
Hiren
  • 12,720
  • 7
  • 52
  • 72
arniotaki
  • 2,175
  • 2
  • 23
  • 26
6
self.mySearchBar.backgroundColor = [UIColor redColor]; 

won't do anything,

but

self.mySearchBar.tintColor = [UIColor redColor]; 

will!

user1927446
  • 1,053
  • 8
  • 7
3
    self.mySearchBar.backgroundVolor = [UIColor redColor];

"Volor" or "Color

  self.mySearchBar.backgroundColor = [UIColor redColor];

Also if you want to tint it:

According to the documentation : https://developer.apple.com/iphone/library/documentation/UIKit/Reference/UISearchBar_Class/Reference/Reference.html, there is a tintColor property on the UISearchBar class.

In the TableSearch example: https://developer.apple.com/library/ios/#samplecode/TableSearch/ the search bar is defined and loaded in the MainView.xib. If you want to change its tintColor or style, just do it in the xib and it will be loaded into the application.

BUT THE ONLY REAL WAY to change background color is to override it, look at the answer from the following question

UISearchBar clear background color or set background image [iphone sdk]

Community
  • 1
  • 1
MCKapur
  • 9,127
  • 9
  • 58
  • 101
2

I'm not sure if it has changed since iOS7, but it seems that the search bar in the search display controller requires an extra loop to properly remove the UISearchBarBackground view.

I created a recursive method to properly clean the search bar.

- (void) removeUISearchBarBackgroundInViewHierarchy:(UIView *)view
{
    for (UIView *subview in [view subviews]) {
        if ([subview isKindOfClass:NSClassFromString(@"UISearchBarBackground")]) {
            [subview removeFromSuperview];
            break; //To avoid an extra loop as there is only one UISearchBarBackground
        } else {
            [self removeUISearchBarBackgroundInViewHierarchy:subview];
        }
    }
}

You can simply send your search bar to the method and change the color afterward, a bit like the suggested answers above.

[self removeUISearchBarBackgroundInViewHierarchy:self.searchDisplayController.searchBar];
self.searchDisplayController.searchBar.backgroundColor = yourUIColor;
Gabriel Cartier
  • 1,734
  • 20
  • 22
2

I had to set UISearchBar's searchBarStyle to .default, otherwise nothing works.

da1
  • 487
  • 5
  • 10
1
searchBar.barTintColor =  UIColor.redColor()
Andy
  • 49,085
  • 60
  • 166
  • 233
clearlight
  • 12,255
  • 11
  • 57
  • 75
0

Try this code it works fine for me.

for( UIView* v in [parent subviews] ) { 
if( [v isKindOfClass:[UISearchBar class]] ) {
  [(UISearchBar*)v  setTintColor:[UIColor blackColor]];
}
Ronak
  • 1
  • 1
0

If you have issues with top and bottom lines try to remove the UISearchBarBackground:

self.searchBar = [[UISearchBar alloc] initWithFrame:CGRectMake(0, 0, self.tableView.frame.size.width, 50)];

for (UIView *subview in self.searchBar.subviews) {
    if ([subview isKindOfClass:NSClassFromString(@"UISearchBarBackground")]) {
        [subview removeFromSuperview];
        break;
    }
    for (UIView *subsubview in subview.subviews) {
        if ([subsubview isKindOfClass:NSClassFromString(@"UISearchBarBackground")]) {
            [subsubview removeFromSuperview];
            break;
        }
    }
}

self.searchBar.barTintColor = [UIColor redColor];
self.searchBar.backgroundColor = [UIColor redColor];
Niko Zarzani
  • 1,372
  • 2
  • 15
  • 28
0

Depends what exactly are you going to change

searchBar.barTintColor
searchBar.tintColor

Other items you are able customise thought some extra code

yoAlex5
  • 29,217
  • 8
  • 193
  • 205