我的 UISearchBar 有以下代码:
 UISearchBar * searchBar = [[UISearchBar alloc] initWithFrame:CGRectMake(0, 0, 300, 44)];
    searchBar.placeholder = @"Search for a tag";
    searchBar.delegate = self;
结果如下:

我想将白色背景更改为清晰的颜色。我该怎么做呢?基本上我希望textField 背景颜色清晰。
我的 UISearchBar 有以下代码:
 UISearchBar * searchBar = [[UISearchBar alloc] initWithFrame:CGRectMake(0, 0, 300, 44)];
    searchBar.placeholder = @"Search for a tag";
    searchBar.delegate = self;
结果如下:

我想将白色背景更改为清晰的颜色。我该怎么做呢?基本上我希望textField 背景颜色清晰。
如果您支持 iOS 5 及更高版本,请执行以下操作:
[self.searchBar setBackgroundImage:[[UIImage alloc] init]];
尝试下面的代码来清除背景颜色:
//to clear searchbar backgraound
- (void) clearSearchBarBg
{
    for (UIView *subview in theSearchBar.subviews) 
    {
        if ([subview isKindOfClass:NSClassFromString(@"UISearchBarBackground")]) 
        {
            [subview removeFromSuperview];
            break;
        }
    }
}
制作透明图像,并将该图像与以下代码一起使用。
[search setSearchFieldBackgroundImage:[UIImage imageNamed:@"bg_search.png"] forState:UIControlStateNormal];
试试这个:
[[searchBar.subviews objectAtIndex:0] removeFromSuperview];
使用此代码:
searchBar.backgroundColor=[UIColor clearColor];
对于 iOS 7,请使用
[searchBar setBarTintColor:[UIColor whiteColor]];
对于 iOS 6,使用
[searchBar setTintColor:[UIColor whiteColor]];
好的,这是一个清晰的背景,“搜索”占位符是灰色的,望远镜图标也是灰色的。
[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];