Question: 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];

```

Answer: 
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;

```
Try this code it works fine for me.

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

```