Original UISearchBar Image

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 want to change the white background to a clear color. How should I do it? Basically, I want the background color of the textField to be transparent .

+5
source share
7 answers

Make a transparent image and use this image using the code below.

[search setSearchFieldBackgroundImage:[UIImage imageNamed:@"bg_search.png"] forState:UIControlStateNormal];
+2
source

If you support iOS 5 and above, just do:

[self.searchBar setBackgroundImage:[[UIImage alloc] init]];
+4
source

, :

//to clear searchbar backgraound
- (void) clearSearchBarBg
{
    for (UIView *subview in theSearchBar.subviews) 
    {
        if ([subview isKindOfClass:NSClassFromString(@"UISearchBarBackground")]) 
        {
            [subview removeFromSuperview];
            break;
        }
    }
}
+3

:

[[searchBar.subviews objectAtIndex:0] removeFromSuperview];
+1

:

searchBar.backgroundColor=[UIColor clearColor];
0

iOS 7, ,

[searchBar setBarTintColor:[UIColor whiteColor]];

ios 6,

[searchBar setTintColor:[UIColor whiteColor]];
0

, , "" , spyglass.

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

All Articles