【问题标题】:UISearchBar: clear background color or set background image [duplicate]UISearchBar:清除背景颜色或设置背景图像[重复]
【发布时间】:2011-01-09 11:51:34
【问题描述】:

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

【问题讨论】:

    标签: ios uisearchbar


    【解决方案1】:

    一种面向未来的方式:

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

    【讨论】:

    • 也在 ios 5 和 ios 6 上工作。在 iPad 上测试
    • 如果你使用 ARC,它会为你发布。
    • 这也应该是答案,IMO!
    • 马丁不!使用ARC该死的。除非你需要超高性能的代码,否则我不相信 ARC 在大多数情况下不会更好。
    • 确认这适用于 iOS 7
    【解决方案2】:

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

    【讨论】:

    • +1 表示在 2011 年仍然准确。我正在寻找您示例的最后五行。干杯!
    • 如果您使用 uiimageview 显示背景图像,并且您想将搜索栏隐藏在导航栏后面,则图像需要 44 px 高度。或者使用clipsToBounds = YES,否则导航栏下方会画出1px的线。
    • 我这样做了: NSClassFromString([NSString stringWithFormat:@"U%@Sea%@", @"I", @"rchBarBackground"]) 以防苹果搜索命名为私有的字符串类..但不知道它是否会有所帮助(?)
    • Apple 会因为使用私有类“UISearchBarBackground”而拒绝此代码吗?
    • 我在一个提交到应用商店的应用中使用了这个并且没有被拒绝。
    【解决方案3】:

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

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

    【讨论】:

    • 从 SDK 4.2 开始,这会破坏按钮和字段。
    • 我错了,只有将这段代码放在 layoutSubviews 中才会这样。呵呵。
    • 不要忘记在for 循环之后设置颜色 - [searchBar setBackgroundColor:[UIColor clearColor]];
    • 此方法不适用于 iOS 5.1 上的 Xcode 4.4.1。
    【解决方案4】:

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

    【讨论】:

      【解决方案5】:

      我刚刚想出了一个非常有效的解决方案。您必须覆盖 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
      

      希望这会有所帮助!

      【讨论】:

      • 对我不起作用...(ios 4.2)
      • 在 iOS 5 上也不适合我
      【解决方案6】:

      您有两个问题:
      1.UISearchBar清除背景色:
      See my answer here

      2.设置背景图片 解决方案:(如果您使用的是 iOS 5.0 +

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

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

      希望这会有所帮助。

      【讨论】:

        【解决方案7】:

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

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

        【讨论】:

        • 这在 Xcode 4.4.1 中不起作用。
        【解决方案8】:

        如何在 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;
        

        【讨论】:

        • 这东西对我来说很好用。 +1 评分。
        【解决方案9】:

        searchBar.searchBarStyle = UISearchBarStyleMinimal;

        【讨论】:

          【解决方案10】:

          【讨论】:

            【解决方案11】:

            使用 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
            

            【讨论】:

              【解决方案12】:

              设置背景图片

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

              【讨论】:

                【解决方案13】:

                一个班轮:

                [[self.theSearchBar.subviews objectAtIndex:0] removeFromSuperview];
                

                【讨论】:

                  猜你喜欢
                  • 1970-01-01
                  • 2012-06-06
                  • 2023-03-25
                  • 1970-01-01
                  • 1970-01-01
                  • 1970-01-01
                  • 1970-01-01
                  • 2012-01-01
                  • 1970-01-01
                  相关资源
                  最近更新 更多