首页 > 其他 > 详细

SearchBar改变背景颜色

时间:2014-04-16 16:17:43      阅读:207      评论:0      收藏:0      [点我收藏+]

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

方法一:

bubuko.com,布布扣
bubuko.com,布布扣
 
    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];

 
bubuko.com,布布扣
bubuko.com,布布扣

方法二:

bubuko.com,布布扣
bubuko.com,布布扣
- (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;
}
bubuko.com,布布扣
bubuko.com,布布扣

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

SearchBar改变背景颜色,布布扣,bubuko.com

SearchBar改变背景颜色

原文:http://www.cnblogs.com/wuwangchuxin/p/3666760.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!