iWizard
iWizard

Reputation: 7094

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

Upvotes: 7

Views: 34794

Answers (9)

yoAlex5
yoAlex5

Reputation: 34175

Depends what exactly are you going to change

searchBar.barTintColor
searchBar.tintColor

Other items you are able customise thought some extra code

Upvotes: 0

da1
da1

Reputation: 547

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

Upvotes: 2

Niko Zarzani
Niko Zarzani

Reputation: 1402

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

Upvotes: 0

arniotaki
arniotaki

Reputation: 2265

Try:

self.mySearchBar.barTintColor = [UIColor redColor];

Upvotes: 29

clearlight
clearlight

Reputation: 12615

searchBar.barTintColor =  UIColor.redColor()

Upvotes: 1

Gabriel Cartier
Gabriel Cartier

Reputation: 1764

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;

Upvotes: 2

user1927446
user1927446

Reputation: 1043

self.mySearchBar.backgroundColor = [UIColor redColor]; 

won't do anything,

but

self.mySearchBar.tintColor = [UIColor redColor]; 

will!

Upvotes: 6

Ronak
Ronak

Reputation: 1

Try this code it works fine for me.

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

Upvotes: 0

MCKapur
MCKapur

Reputation: 9157

    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]

Upvotes: 3

Related Questions