Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to change color of Search Bar?

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];
like image 230
iWizard Avatar asked May 10 '12 11:05

iWizard


1 Answers

Try:

self.mySearchBar.barTintColor = [UIColor redColor];
like image 119
arniotaki Avatar answered Sep 30 '22 18:09

arniotaki