UISearchBar去除背景颜色

去除颜色的方法是讲backgroundImage替换成一个自己的Image,我们可以自己通过CG库的方法生成一张无色背景,这样就可以了
   UISearchBar * SearchBar = [[UISearchBar alloc]initWithFrame:CGRectMake(self.frame.size.width - 250, 20, 200, 30)];
    SearchBar.placeholder = @"请输入查询内容";
//    SearchBar.backgroundColor = RandomColor;
    SearchBar.backgroundImage = [self imageWithColor:[UIColor clearColor]];
    [self addSubview:SearchBar];
下面是生成一个无色背景的方法
- (UIImage *)imageWithColor:(UIColor *)color
{
    CGRect rect = CGRectMake(0, 0, 1, 1);
    UIGraphicsBeginImageContext(rect.size);
    CGContextRef context = UIGraphicsGetCurrentContext();
    CGContextSetFillColorWithColor(context, [color CGColor]);
    CGContextFillRect(context, rect);
    UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return image;
}


你可能感兴趣的:(iOS)