自定义UISearchBar输入框背景颜色

//设置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;
}

原始效果


屏幕快照 2018-06-04 11.49.54.png

效果图


屏幕快照 2018-06-04 11.50.33.png

你可能感兴趣的:(自定义UISearchBar输入框背景颜色)