2010-08-16 32 views

回答

15

您可以覆蓋搜索欄的drawRect。通過將「UISearchBarBackground」視圖設置爲0的alpha值,它將爲您設置的平坦色調顏色繪製它。例如,在應用程序委託補充一點:

@implementation UISearchBar(CustomBackground) 
- (void)drawRect:(CGRect)rect 
{ 
    // Find the UISearchBarBackground view and set it's alpha to 0 so we get a "flat" 
    // search bar. 
    for(UIView *subview in self.subviews) 
    { 
     if([subview isKindOfClass:NSClassFromString(@"UISearchBarBackground")]) 
     { 
      [subview setAlpha:0.0F]; 
      break; 
     } 
    } 
} 
@end 
+0

添加這會導致我的搜索欄背景變黑,而不是我嘗試應用的顏色。任何想法爲什麼? – jmosesman 2012-07-16 18:33:12

+0

我還需要更改搜索欄的背景顏色,在UISearchBar上給出所需的純色,謝謝。 – 2013-04-01 09:18:47

0

我這樣做:

[toolbar setTintColor:[UIColor greenColor]]; 

,並得到這樣的:

alt text http://gallery.me.com/davedelong/100084/Screen%20shot%202010-08-16%20at%202.44.07%20PM/web.png?ver=12819951050001

這就是你想要的?

+0

差不多,但它並不適用於一個UISearchBar – 2010-08-17 03:38:13

+0

我還沒有嘗試過的代碼的工作,但在IB設置着色顏色正常工作(但不不解決純色問題,仍然有一個漸變圖層) – 2013-04-01 07:21:51

2
 _tempSearchBar =[[UISearchBar alloc]initWithFrame:CGRectMake(44, 0, 320 - 44, 43)]; 
     _tempSearchBar.barStyle=UIBarStyleDefault; 
     [email protected]"搜索"; 
     for(UIView *subview in _tempSearchBar.subviews) 
     { 
      if([subview isKindOfClass:NSClassFromString(@"UISearchBarBackground")]) 
      { 
       UIView *aView = [[UIView alloc] initWithFrame:subview.bounds]; 
       aView.backgroundColor = [UIColor greenColor]; 
//    [subview setAlpha:0.0f]; 
       [subview addSubview:aView]; 
       break; 
      } 
     }