2012-06-01 54 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];