自定义UISearchBar输入框背景颜色
2018-06-04 本文已影响16人
吃货_X
//设置searchBar的背景颜色
self.searchBar.backgroundImage = [XLBTools createImageWithColor:COLOR(whiteColor)];
//获得searchBar的输入框
UITextField *textField = [self.searchBar valueForKeyPath:@"_searchField"];
//做你想要得设置(有些设置不一定能成功)
textField.backgroundColor = GetColor(230, 230, 230, 1);
textField.clearButtonMode = UITextFieldViewModeNever;
[self.searchBar setValue:textField forKeyPath:@"_searchField"];
#pragma mark - UIColor 转UIImage
+ (UIImage*)createImageWithColor: (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;
}
原始效果

效果图
