UISearchBar 改变背景+切圆角+改高度
直接上代码
- (UISearchBar *)searchBar {
if (!_searchBar) {
_searchBar = [[UISearchBar alloc]initWithFrame:CGRectMake(46, 6 +JX_STATUS_Height, JX_SCREEN_WIDTH-95, 32)];
_searchBar.delegate = self;
_searchBar.placeholder = @"查找视频、用户和商品";
/* 以下是核心代码*/
//设置TextField高度
[_searchBar setSearchFieldBackgroundImage:[self GetImageWithColor:JX_LightTextfieldColorF5F5F5 andHeight:32] forState:UIControlStateNormal];
//去除上下边线
[_searchBar setSearchBarStyle:UISearchBarStyleMinimal];
//搜索框样式
UITextField *seachTextFild;;
if (@available(iOS 13.0, *)) {
seachTextFild = _searchBar.searchTextField;
}else{
seachTextFild = [_searchBar valueForKey:@"_searchField"];
}
seachTextFild.layer.cornerRadius = 16.0;
seachTextFild.layer.masksToBounds = YES;
}
return _searchBar;
}
- (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;
}