Reputation: 33674
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:
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.
Upvotes: 5
Views: 6500
Reputation: 312
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];
Upvotes: 0
Reputation: 51
for iOS 7, please use
[searchBar setBarTintColor:[UIColor whiteColor]];
for ios 6, use
[searchBar setTintColor:[UIColor whiteColor]];
Upvotes: 0
Reputation: 4180
If you are supporting iOS 5 and above just do:
[self.searchBar setBackgroundImage:[[UIImage alloc] init]];
Upvotes: 4
Reputation: 2008
use this code:
searchBar.backgroundColor=[UIColor clearColor];
Upvotes: 0
Reputation: 1263
Make transparent Image, and use that image with below code.
[search setSearchFieldBackgroundImage:[UIImage imageNamed:@"bg_search.png"] forState:UIControlStateNormal];
Upvotes: 2
Reputation: 3907
Try this:
[[searchBar.subviews objectAtIndex:0] removeFromSuperview];
Upvotes: 1
Reputation: 19418
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;
}
}
}
Upvotes: 3