2012-03-20 133 views

回答

4

東陽UI搜索欄已經得到了自己的背景來看,這是黑色的,如果我們刪除該視圖然後的UISearchBar的背景會變得清晰:

for (UIView *subview in mySearchBar.subviews) { 
    if ([subview isKindOfClass:NSClassFromString(@"UISearchBarBackground")]) { 
     [subview removeFromSuperview]; 
     break; 
    } 
} 
+0

它只是創造黑色的背景,我需要顯示導航背景顏色 – Hiren 2012-03-20 06:00:46

+0

我改變了我的答案後..我檢查它在我的代碼...現在它工作正常... – 2012-03-20 06:23:03

+0

是否searchDisplayControllers內這項工作?我不能讓這與iOS 6.1工作 – powerj1984 2013-06-13 17:43:01

-1

嘗試用這一行:

[[ [searchBar subviews] objectAtIndex:0] removeFromSuperview]; 
[[ [searchBar subviews] objectAtIndex:0] setBackgroundColor:[UIColor yellowColor]]; 

肯定會幫助你。

2

//搜索欄(使用背景圖片)

UISearchBar* bar = [[UISearchBar alloc] initWithFrame:CGRectMake(568, 30, 200, 44)]; 
    UIImageView* iview = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"blue.png"]]; 
    iview.frame = CGRectMake(0, 0, 200, 44); 

    [bar insertSubview:iview atIndex:1]; 
    [iview release]; 
    [self.view addSubview:bar]; 
    [bar release]; 

OR(使用彩色)

//搜索欄

UISearchBar* bar = [[UISearchBar alloc] initWithFrame:CGRectMake(568, 30, 200, 44)]; 

[[[bar subviews] objectAtIndex:0] setAlpha:0.0]; 

bar.tintColor = [UIColor colorWithRed:.376f green:.386f blue:.452f alpha:1.0]; 
    bar.clipsToBounds = YES; 
[self.view addSubview:bar]; 
[bar release]; 
+0

Thnanks爲您的答案 – SampathKumar 2013-03-25 14:32:56

0

清晰的背景顏色(如果您想要圖像設置,然後使用評論代碼)

for (UIView *subview in searchBar.subviews) 
{ 
    if ([subview isKindOfClass:NSClassFromString(@"UISearchBarBackground")]) 
    { 
     // if You want set iamge then use comment code 
     /* UIView *backgroundView = [[UIView alloc] initWithFrame:subview.frame]; 
     bg.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"searchBar"]]; 
     [self.searchBar insertSubview:backgroundView aboveSubview:subview];*/ 

     [subview removeFromSuperview]; 
    } 
} 
0

對於清除搜索欄的背景視圖:

[[[self.searchBar subviews] objectAtIndex:0] setHidden:YES]; 
    for (id subview in self.searchBar.subviews) { 
     if ([subview isKindOfClass:NSClassFromString(@"UISearchBarBackground")]) { 
      [subview removeFromSuperview]; 
     } 

    } 
2

設置爲的UISearchBar背景圖片如下。

[searchBar setBackgroundImage:[UIImage new]]; 

現在UISearchBar背景色將消失。也可以在iOS 7.1中工作

+0

我發現這是最簡單和最乾淨的解決方案。在iOS 8.1中工作。 – Alexander 2014-11-20 15:34:51

0

我在iOS8中發現,這可以更好地將UISearchBar簡化爲文本字段。這將允許您擺脫背景顏色。

for (UIView *subView in self.searchBar.subviews) { 
    for (UIView *secondLevelSubview in subView.subviews) { 
     if (![secondLevelSubview isKindOfClass:NSClassFromString(@"UISearchBarTextField")]) { 
      [secondLevelSubview removeFromSuperview]; 
     } 
    } 
}