UISearchBar: clear background color or set backgro

2020-01-27 11:32发布

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

13条回答
放荡不羁爱自由
2楼-- · 2020-01-27 12:14

Set background image

    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];
查看更多
看我几分像从前
4楼-- · 2020-01-27 12:15

One liner:

[[self.theSearchBar.subviews objectAtIndex:0] removeFromSuperview];
查看更多
等我变得足够好
5楼-- · 2020-01-27 12:18

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.

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

Hope this helps!

查看更多
孤傲高冷的网名
6楼-- · 2020-01-27 12:23

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

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

// Show
[[self.theSearchBar.subviews objectAtIndex:0] setAlpha:1.0];
查看更多
太酷不给撩
7楼-- · 2020-01-27 12:23

How to set background color in UISearchBar:

#import <QuartzCore/QuartzCore.h>

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

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

self.tweetSearchbar.layer.backgroundColor = [UIColor blueColor].CGColor;
查看更多
登录 后发表回答