UISearchBar:清除背景颜色或设置背景图像

Mom*_*omi 41 uisearchbar ios

如何设置搜索栏的背景图像或清除背景,如笔记应用程序?

Rud*_*vič 175

面向未来的方式:

[searchBar setBackgroundImage:[UIImage new]];
[searchBar setTranslucent:YES];
Run Code Online (Sandbox Code Playgroud)

  • 马丁不!使用ARC dammit.除非你需要超级高效的代码,否则我不相信ARC在大多数情况下都不会更好. (8认同)
  • 确认这适用于iOS 7 (4认同)

kat*_*yte 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;
    }
}   
Run Code Online (Sandbox Code Playgroud)

  • +1在2011年仍然准确.我正在寻找你的例子的最后五行.干杯! (3认同)

mj_*_*mj_ 14

我有问题,上面的答案.我使用了以下内容.

for (UIView *subview in searchBar.subviews) {
    if ([subview isKindOfClass:NSClassFromString(@"UISearchBarBackground")]) {
        [subview removeFromSuperview];
        break;
    }
}
Run Code Online (Sandbox Code Playgroud)

  • 不要忘记在`for`循环后设置颜色 - `[searchBar setBackgroundColor:[UIColor clearColor]];` (3认同)

Chr*_*ris 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;
        }
    }   
}
Run Code Online (Sandbox Code Playgroud)


Des*_*erd 8

我想出了一个非常好的解决方案.您必须覆盖UISearchBar,然后隐藏背景和分段控制图层.然后绘制背景.

@ .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
Run Code Online (Sandbox Code Playgroud)

@ .H

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


@interface UISearchBar(CustomBackground) 

@end
Run Code Online (Sandbox Code Playgroud)

希望这可以帮助!

  • 在iOS 5上也不适用于我 (2认同)

the*_*ign 8

这里有两个问题:
1.UISearchBar清晰的背景颜色:
请在此处查看我的答案

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

[[UISearchBar appearance]setSearchFieldBackgroundImage:[navBarGradImage resizableImageWithCapInsets:inset2] forState:UIControlStateNormal];
Run Code Online (Sandbox Code Playgroud)

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

希望这可以帮助.