How to change the color of the search bar?

I have implemented a search bar and I want to change the color of the search bar. How can i do this?

I tried:

self.mySearchBar.backgroundColor = [UIColor redColor];

but without success.

UPDATE (enabled):

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

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

... so the code will be as follows:

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

  self.mySearchBar.backgroundColor = [UIColor redColor];
+5
source share
7 answers

Try:

self.mySearchBar.barTintColor = [UIColor redColor];
+22
source
self.mySearchBar.backgroundColor = [UIColor redColor]; 

will do nothing

but

self.mySearchBar.tintColor = [UIColor redColor]; 

will be!

+5
source
    self.mySearchBar.backgroundVolor = [UIColor redColor];

"" "

  self.mySearchBar.backgroundColor = [UIColor redColor];

, :

: https://developer.apple.com/iphone/library/documentation/UIKit/Reference/UISearchBar_Class/Reference/Reference.html, UISearchBar tintColor.

TableSearch: https://developer.apple.com/library/ios/#samplecode/TableSearch/ MainView.xib. tintColor , xib .

- ,

UISearchBar [iphone sdk]

+2

, iOS7, , UISearchBarBackground.

.

- (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 submit your search bar to the method and change the color later, a little like the suggested answers above.

[self removeUISearchBarBackgroundInViewHierarchy:self.searchDisplayController.searchBar];
self.searchDisplayController.searchBar.backgroundColor = yourUIColor;
+2
source
searchBar.barTintColor =  UIColor.redColor()
+1
source

Try this code, it works great for me.

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

If you have problems with the top and bottom lines, try removing 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];
0
source

All Articles