41

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

4

13 回答 13

175

面向未来的方式:

[searchBar setBackgroundImage:[UIImage new]];
[searchBar setTranslucent:YES];
于 2012-09-26T09:34:15.703 回答
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;
    }
}   
于 2010-10-03T04:44:04.433 回答
14

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

for (UIView *subview in searchBar.subviews) {
    if ([subview isKindOfClass:NSClassFromString(@"UISearchBarBackground")]) {
        [subview removeFromSuperview];
        break;
    }
}
于 2010-09-24T14:12:14.533 回答
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;
        }
    }   
}
于 2011-05-29T22:59:04.547 回答
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

@ 。H

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


@interface UISearchBar(CustomBackground) 

@end

希望这可以帮助!

于 2011-04-05T19:18:00.233 回答
8

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

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

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

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

希望这可以帮助。

于 2012-05-18T06:10:59.293 回答
4

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

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

// Show
[[self.theSearchBar.subviews objectAtIndex:0] setAlpha:1.0];
于 2012-03-15T21:32:12.257 回答
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;
于 2012-03-28T15:41:54.157 回答
4

searchBar.searchBarStyle = UISearchBarStyleMinimal;

于 2014-02-03T21:08:24.873 回答
2

看这里:UISearchbar 背景图片更改

于 2010-03-27T11:33:32.980 回答
2

随着 iOS8 sdks 苹果移动@“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
于 2014-12-11T11:30:46.193 回答
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];
于 2011-09-21T08:46:38.497 回答
0

一个班轮:

[[self.theSearchBar.subviews objectAtIndex:0] removeFromSuperview];
于 2011-10-18T05:47:21.280 回答