zoukankan      html  css  js  c++  java
  • SearchBar改变背景颜色

    在许多需求下需要改变SearchBar的背景图片今天试了一下代码如下:

    方法一:

    复制代码
     
        UISearchBar *searchBar=[[UISearchBar alloc]initWithFrame:CGRectMake(0, 100, 320, 44)];
        [self.view addSubview:searchBar];
        
        for (UIView *obj in [searchBar subviews]) {
            for (UIView *objs in [obj subviews]) {
                if ([objs isKindOfClass:NSClassFromString(@"UISearchBarBackground")]){
                    [objs removeFromSuperview];
                }
            }
            if ([obj isKindOfClass:NSClassFromString(@"UISearchBarBackground")]){
                [obj removeFromSuperview];
            }
        }

       searchBar.backgroundColor=[UIColor colorWithRed:0.9686 green:0.9686 blue:0.9686 alpha:1.0];

     
    复制代码

    方法二:

    复制代码
    - (void)viewDidLoad
    {
        [super viewDidLoad];
    
        UISearchBar *searchBar=[[UISearchBar alloc]initWithFrame:CGRectMake(0, 100, 320, 44)];
        [self.view addSubview:searchBar];
        searchBar.backgroundImage = [self imageWithColor:[UIColor clearColor]];
        
    }
    - (UIImage *)imageWithColor:(UIColor *)color
    {
        CGRect rect = CGRectMake(0.0f, 0.0f, 1.0f, 1.0f);
        UIGraphicsBeginImageContext(rect.size);
        CGContextRef context = UIGraphicsGetCurrentContext();
        
        CGContextSetFillColorWithColor(context, [color CGColor]);
        CGContextFillRect(context, rect);
        
        UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
        UIGraphicsEndImageContext();
        
        return image;
    }
    复制代码

    这两种方法都可以改变searchBar 的背景颜色,也可以通过断点调试来查看苹果内部信息。

  • 相关阅读:
    HDU 5818 Joint Stacks
    HDU 5816 Hearthstone
    HDU 5812 Distance
    HDU 5807 Keep In Touch
    HDU 5798 Stabilization
    HDU 5543 Pick The Sticks
    Light OJ 1393 Crazy Calendar (尼姆博弈)
    NEFU 2016省赛演练一 I题 (模拟题)
    NEFU 2016省赛演练一 F题 (高精度加法)
    NEFU 2016省赛演练一 B题(递推)
  • 原文地址:https://www.cnblogs.com/wuwangchuxin/p/3666760.html
Copyright © 2011-2022 走看看