IOS 改变UISearchBar的背景色

之前网上提供的方法试了很多种  都不能很好的去掉背景色  ,修改背景色方法如下:

 

searchbar.barStyle = UIBarStyleBlackTranslucent;

    searchbar.autocorrectionType = UITextAutocorrectionTypeNo;

    searchbar.autocapitalizationType = UITextAutocapitalizationTypeNone;

    searchbar.keyboardType = UIKeyboardTypeDefault;

searchbar.backgroundImage = [self SearchImageWithColor:[UIColor clearColor] size:searchbar.bounds.size];
 
定义一个函数  
//取消searchbar背景色
- (UIImage *)SearchImageWithColor:(UIColor *)color size:(CGSize)size
{
    CGRect rect = CGRectMake(0, 0, size.width, size.height);
    UIGraphicsBeginImageContext(rect.size);
    CGContextRef context = UIGraphicsGetCurrentContext();
      
    CGContextSetFillColorWithColor(context, [color CGColor]);
    CGContextFillRect(context, rect);
      
    UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
      
    return image;
}
posted @ 2016-01-27 10:54  预见者  阅读(445)  评论(0编辑  收藏  举报