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

ここに2つの質問があります:
1.UISearchBarクリア背景色:
ここで私の答えを参照してください

2.背景画像を設定する解決策:( iOS5.0以降を使用している場合

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

注:背景として透明な画像を使用することもできます。

お役に立てれば。

于 2012-05-18T06:10:59.293 に答える
8

本当にうまくいく解決策を思いつきました。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

お役に立てれば!

于 2011-04-05T19:18:00.233 に答える
4

オンデマンドで非表示/表示できるように、アルファを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

searchBar.searchBarStyle = UISearchBarStyleMinimal;

于 2014-02-03T21:08:24.873 に答える
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 に答える
2

こちらをご覧ください:UISearchbarの背景画像の変更

于 2010-03-27T11:33:32.980 に答える
2

iOS8 SDK では、Apple は @"UISearchBarBackground" ビューを 1 レベル下に移動したため、次のように子ビューのサブビューを確認する必要があります。

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 に答える