2012-06-01 52 views
5

我已经为我的UISearchBar下面的代码:的UISearchBar清晰的背景图像

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

结果如下:

enter image description here

我想白色背景更改为清晰颜色。我该怎么做呢?基本上我想要textField背景颜色清晰

+0

请尝试[这里] [1] [1]:http://stackoverflow.com/questions/2139115/uisearchbar-clear-background-color-or-set-background-像iphone-SDK 希望它会帮助你.... 感谢 –

+0

我最近面对这个问题,我已经不是设置透明背景图像的不同的解决方案http://stackoverflow.com/questions/37158702/ ios-uisearchbar-background-color-in-ios-9 –

回答

2

使透明图像,并使用该图像具有低于码。

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

这是否对你有用吗?请回复并注明是否对你有帮助... – jaym

+2

我发现销毁的搜索栏文本字段不是背景,应该是: '[searchBar setBackgroundImage:[UIImage imageNamed:@“bg_search.png”] ];' –

0

使用此代码:

searchBar.backgroundColor=[UIColor clearColor]; 
+0

我试过了它没有工作 – adit

3

尝试下面的代码来清除背景色:

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

试过这个,它没有工作 – adit

+0

好的尝试。虽然透明PNG工程: '[searchBar setBackgroundImage:[UIImage imageNamed:@“bg_search.png”]];' –

1

尝试这种情况:

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

这并没有工作以及 – adit

4

如果要支持的iOS 5及以上只是做:

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

太棒了 - 比透明图像文件中的链接好得多。 –

0

为iOS 7,请使用

[searchBar setBarTintColor:[UIColor whiteColor]]; 

为iOS 6,使用

[searchBar setTintColor:[UIColor whiteColor]]; 
0

好吧,这是一个清晰的背景,“搜索”占位符是灰色的,并且是望远镜图标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];