5

I have the following code for my UISearchBar:

 UISearchBar * searchBar = [[UISearchBar alloc] initWithFrame:CGRectMake(0, 0, 300, 44)];
    searchBar.placeholder = @"Search for a tag";
    searchBar.delegate = self;

The result is as follows:

enter image description here

I'd like to change the white background to a clear color. How do I do this? Basically I want the textField background color to be clear.

adit
  • 32,574
  • 72
  • 229
  • 373
  • Please try [this][1] [1]: http://stackoverflow.com/questions/2139115/uisearchbar-clear-background-color-or-set-background-image-iphone-sdk Hope it will helps you.... Thanks – Mitesh Khatri Jun 01 '12 at 06:18
  • I face this problem recently , I have a different solution than setting transparent background image http://stackoverflow.com/questions/37158702/ios-uisearchbar-background-color-in-ios-9 – Stephen Chen May 12 '16 at 15:47

7 Answers7

4

If you are supporting iOS 5 and above just do:

[self.searchBar setBackgroundImage:[[UIImage alloc] init]];
3lvis
  • 4,190
  • 1
  • 30
  • 35
3

Try below code to clear background color :

//to clear searchbar backgraound
- (void) clearSearchBarBg
{
    for (UIView *subview in theSearchBar.subviews) 
    {
        if ([subview isKindOfClass:NSClassFromString(@"UISearchBarBackground")]) 
        {
            [subview removeFromSuperview];
            break;
        }
    }
}
Maulik
  • 19,348
  • 14
  • 82
  • 137
  • Good try. The transparent png hack works though: `[searchBar setBackgroundImage: [UIImage imageNamed:@"bg_search.png"] ];` – Warren P Apr 10 '13 at 15:13
2

Make transparent Image, and use that image with below code.

[search setSearchFieldBackgroundImage:[UIImage imageNamed:@"bg_search.png"] forState:UIControlStateNormal];
jaym
  • 1,253
  • 13
  • 18
  • 2
    I found that destroyed the search bar text field not the background, it should have been: `[searchBar setBackgroundImage: [UIImage imageNamed:@"bg_search.png"] ];` – Warren P Apr 10 '13 at 15:13
1

Try this:

[[searchBar.subviews objectAtIndex:0] removeFromSuperview];
Hector
  • 3,909
  • 2
  • 30
  • 46
0

use this code:

searchBar.backgroundColor=[UIColor clearColor];
Peter DeWeese
  • 18,141
  • 8
  • 79
  • 101
Wasif Saood
  • 1,998
  • 1
  • 11
  • 12
0

for iOS 7, please use

[searchBar setBarTintColor:[UIColor whiteColor]];

for ios 6, use

[searchBar setTintColor:[UIColor whiteColor]];
Jacky Li
  • 51
  • 2
0

Ok, this is a clear background, the "Search" placeholder is grey and as is the spyglass icon thingy.

[self.searchBar setPlaceholder:@"Search          "; //the spaces push it to left align as there is no left align for the UISearchBar
[self.searchBar setBackgroundImage:[UIImage imageNamed:@"thatSearchIconThing"] forBarPosition:UIBarPositionTop barMetrics:UIBarMetricsLandscapePhone]; //my app is landscape
[self.search setSearchBarStyle:UISearchBarStyleMinimal];
TomG103
  • 311
  • 2
  • 26