您的位置:首页 > 产品设计 > UI/UE

iOS 通过设置颜色去除UISearchBar的灰色背景

2016-12-23 16:27 591 查看
实现效果前:



实现效果后:



实现代码如下:

@property (weak,
nonatomic) IBOutlet
UISearchBar *searchbar;

通过UIImage* searchBarBg;调用下面的代码

UIImage* searchBarBg = [self GetImageWithColor:[UIColor
clearColor]
andHeight:32.0f];

[self.searchbar
setBackgroundImage:searchBarBg];

#pragma mark 实现搜索条背景透明化

- (UIImage*) GetImageWithColor:(UIColor*)color andHeight:(CGFloat)height{

    CGRect r=
CGRectMake(0.0f,
0.0f, 1.0f, height);

    UIGraphicsBeginImageContext(r.size);

    CGContextRef context =
UIGraphicsGetCurrentContext();

    CGContextSetFillColorWithColor(context, [color
CGColor]);

    CGContextFillRect(context, r);

    UIImage *img =
UIGraphicsGetImageFromCurrentImageContext();

    UIGraphicsEndImageContext();

    return img;

}
通过这些便实现了去除UISearchBar的灰色背景
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: