2012-03-20 104 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]; 
     } 
    } 
}