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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2139115/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-30 16:49:53  来源:igfitidea点击:

UISearchBar: clear background color or set background image

iosuisearchbar

提问by Momi

How can set the background image, or clear the background, of a search bar, like the note application?

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

采纳答案by Designerd

I just came up with a solution that works really well. You have to override the UISearchBar and then hide both the Background and Segment Control layers. Then Draw the background.

我刚刚想出了一个非常有效的解决方案。您必须覆盖 UISearchBar,然后隐藏 Background 和 Segment Control 层。然后绘制背景。

@ .m

@.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

@ 。H

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


@interface UISearchBar(CustomBackground) 

@end

Hope this helps!

希望这可以帮助!

回答by Rudolf Adamkovi?

A future-proof way:

一种面向未来的方式:

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

回答by katbyte

mj_ has the answer that i used. i was just going to comment as such but i can't yet. So i'll just post my own answer with my implementation where I add a search bar to the top of a table view with a semi-transparent BG.

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;
    }
}   

回答by mj_

I had problems w/ the answer above. I used the following.

我在上面的答案中遇到了问题。我使用了以下内容。

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

回答by Chris

This worked for me (ios4.2+)

这对我有用(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;
        }
    }   
}

回答by thesummersign

You have two questions here:
1.UISearchBar clear background color:
See my answer here

你在这里有两个问题:
1.UISearchBar 清晰的背景颜色:
在这里看到我的答案

2.Set background image Solution:(If you are in iOS 5.0 +)

2.设置背景图片解决方法:(如果你是iOS 5.0+

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

NOTE: You can also try using a transparent image as a background.

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

Hope this helps.

希望这可以帮助。

回答by Brad Thomas

searchBar.searchBarStyle = UISearchBarStyleMinimal;

searchBar.searchBarStyle = UISearchBarStyleMinimal;

回答by Michael Camden

I prefer to just set the alpha to 0 so you can hide/show on demand.

我更喜欢将 alpha 设置为 0,这样您就可以按需隐藏/显示。

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

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

回答by iGautam

How to set background color in UISearchBar:

如何在 UISearchBar 中设置背景颜色:

#import <QuartzCore/QuartzCore.h>

Then make an outlet connection to search bar (say, objSearchbar), and use these lines :

然后将出口连接到搜索栏(例如,objSearchbar),并使用以下几行:

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

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