2010-01-26 166 views

回答

7

我刚想出了一个很好的解决方案。您必须重写UISearchBar,然后隐藏Background和Segment Control层。然后绘制背景。

@ .M

#import "UISearchBar.h" 
#import <QuartzCore/QuartzCore.h> 

@implementation UISearchBar(CustomBackground) 

- (id)init 
    { 
     for (UIView * subview in self.subviews) 
     { 
      if ([subview isKindOfClass:NSClassFromString(@"UISearchBarBackground") ]) 
       subview.alpha = 0.0; 

      if ([subview isKindOfClass:NSClassFromString(@"UISegmentedControl") ]) 
       subview.alpha = 0.0; 
     } 

     return self; 
    } 

+ (UIImage *) bgImagePortrait 
    { 
     static UIImage *image = nil; 
     if (image == nil) 
      image = [[UIImage imageNamed:@"UISearchBarBack.png"] retain ]; 

     return image; 
    } 

+ (UIImage *) bgImageLandscape 
    { 
     static UIImage *image = nil; 
     if (image == nil) 
      image = [[UIImage imageNamed:@"UISearchBarBack.png"] retain]; 

     return image; 
    } 

- (void) drawLayer:(CALayer *)layer inContext:(CGContextRef)contenxt 
    { 
     if ([self isMemberOfClass:[UISearchBar class]] == NO) 
      return; 

     UIImage * image = (self.frame.size.width > 320) ? [UISearchBar bgImageLandscape ] : [UISearchBar bgImagePortrait ]; 

     for (UIView * subview in self.subviews) { 
      if ([subview isKindOfClass:NSClassFromString(@"UISearchBarBackground") ]) 
       subview.alpha = 0.0; 

      if ([subview isKindOfClass:NSClassFromString(@"UISegmentedControl") ]) 
       subview.alpha = 0.0; 
     } 

     CGContextTranslateCTM(contenxt , 0 , image.size.height); 
     CGContextScaleCTM(contenxt, 1.0, -1.0); 
     CGContextDrawImage(contenxt , CGRectMake(0 , 0 , image.size.width , image.size.height), image.CGImage); 
    } 

@end 

@ .H

#import <Foundation/Foundation.h> 
#import <QuartzCore/QuartzCore.h> 


@interface UISearchBar(CustomBackground) 

@end 

希望这有助于!

+1

不适用于我...(ios 4.2) – Chris 2011-05-27 06:23:30

+2

也适用于我的iOS 5 – jjxtra 2012-01-12 22:55:02

+0

不工作iOS 7 – fnc12 2014-09-03 10:12:36

14

我有问题瓦特/以上问题的答案。我使用了以下内容。

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

作为SDK 4.2,这打破了按钮和领域。 – 2010-11-30 04:30:52

+0

我错了,它只会这样做,如果你把这个代码在layoutSubviews。咄。 – 2010-11-30 04:41:26

+3

不要忘记在'for'循环后设置你的颜色 - '[searchBar setBackgroundColor:[UIColor clearColor]];' – beryllium 2012-02-16 14:04:52

29

mj_有我使用的答案。我只是想评论,但我还不能。所以我只会发布我自己的答案,在我的实现中,我将一个搜索栏添加到具有半透明BG的表视图的顶部。

DLog(@" Add search bar to table view"); 

//could be a UIImageView to display an image..? 
bg = [[[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 45)] autorelease]; 
bg.backgroundColor  = [UIColor blackColor]; 
UISearchBar *sb   = [[[UISearchBar alloc]initWithFrame:CGRectMake(0, 0, 290, 45)] autorelease]; 
sb.barStyle    = UIBarStyleBlackTranslucent; 
sb.showsCancelButton  = NO; 
sb.autocorrectionType  = UITextAutocorrectionTypeNo; 
sb.autocapitalizationType = UITextAutocapitalizationTypeNone; 
sb.delegate    = self; 
[bg addSubview:sb]; 
table.tableHeaderView  = bg; 

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

+1在2011年仍然准确。我正在寻找你的例子的最后五行。干杯! – epologee 2011-01-25 21:20:36

+0

如果您使用uiimageview显示背景图像,并且想要隐藏导航栏后面的搜索栏,则图像需要为44像素高。或者使用clipsToBounds = YES,否则会在导航栏下绘制1 px线。 – Olof 2011-07-25 09:32:39

+0

我这样做:NSClassFromString([NSString stringWithFormat:@“U%@ Sea%@”,@“I”,@“rchBarBackground”])在苹果会搜索字符串命名他们的私人类..但不知道它是否有帮助(?) – hfossli 2011-08-19 13:29:10

12

这个工作对我(的iOS4.2 +)

// Change the search bar background 
-(void)viewWillAppear:(BOOL)animated { 
    for (UIView *subview in self.searchBar.subviews) { 
     if ([subview isKindOfClass:NSClassFromString(@"UISearchBarBackground")]) { 
      UIView *bg = [[UIView alloc] initWithFrame:subview.frame]; 
      bg.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"search_bar_bg"]]; 
      [self.searchBar insertSubview:bg aboveSubview:subview]; 
      [subview removeFromSuperview]; 
      break; 
     } 
    } 
} 
+0

不错的一个 - 谢谢克里斯:-) – TheTiger 2012-07-04 07:33:14

+0

我应该在哪里放置? – 2012-07-13 05:41:19

1

设置背景图片

UIImageView *backgroundView = [[UIImageView alloc] initWithFrame:searchBar.bounds]; 
    backgroundView.image = [UIImage imageNamed:@"xxxx.png"]; 
    [searchBar insertSubview:backgroundView atIndex:1]; // at index 1 but not 0 
    [backgroundView release]; 
0

一个内胆:

[[self.theSearchBar.subviews objectAtIndex:0] removeFromSuperview]; 
4

我更愿意只设置alpha为0所以你可以隐藏/显示需求。

// Hide 
[[self.theSearchBar.subviews objectAtIndex:0] setAlpha:0.0]; 

// Show 
[[self.theSearchBar.subviews objectAtIndex:0] setAlpha:1.0]; 
+0

这在Xcode 4.4.1中不起作用。 – 2012-09-26 13:04:24

4

如何设置的UISearchBar背景色:

#import <QuartzCore/QuartzCore.h> 

然后再做一个出口连接到搜索栏(比如,objSearchbar),并使用这些行:

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

self.tweetSearchbar.layer.backgroundColor = [UIColor blueColor].CGColor; 
+0

这件事对我来说工作得很好。 +1评分。 – Nirav 2013-01-08 06:00:04

7

你有这里有两个问题:
1.UISearchBar清除背景颜色:
See my answer here

2.设置背景图片 解决方案:(如果你在安装iOS 5.0 +

[[UISearchBar appearance]setSearchFieldBackgroundImage:[navBarGradImage resizableImageWithCapInsets:inset2] forState:UIControlStateNormal]; 

注意:您也可以尝试使用透明图像作为背景。

希望这会有所帮助。

173

面向未来的方式:

[searchBar setBackgroundImage:[UIImage new]]; 
[searchBar setTranslucent:YES]; 
+1

+1简洁优雅。 – 2012-10-07 12:57:59

+1

也适用于ios 5&ios 6。经过IPad测试 – 2013-01-25 14:36:12

+0

+1我喜欢这种方式 – Imran 2013-03-15 10:38:22

4

搜索栏。searchBarStyle = UISearchBarStyleMinimal;

1

与iOS8上的SDK苹果移动@“UISearchBarBackground”视图更深一层,于是纷纷将需要看的儿童意见波纹管子视图,

for (UIView *subview in searchBar.subviews) { 
     for(UIView* grandSonView in subview.subviews){ 
      if ([grandSonView isKindOfClass:NSClassFromString(@"UISearchBarBackground")]) { 
       grandSonView.alpha = 0.0f; 
      }else if([grandSonView isKindOfClass:NSClassFromString(@"UISearchBarTextField")]){ 
       NSLog(@"Keep textfiedld bkg color"); 
      }else{ 
       grandSonView.alpha = 0.0f; 
      } 
     }//for cacheViews 
    }//subviews