设置背景色
将颜色转为图片
- (UIImage *)imageWithColor:(UIColor *)color size:(CGSize)size {
CGRect rect = CGRectMake(0.0f, 0.0f, size.width, size.height);
UIGraphicsBeginImageContext(size);
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextSetFillColorWithColor(context, [color CGColor]);
CGContextFillRect(context, rect);
UIImage *theImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return theImage;
}
将颜色转成的Image设置为UISearchBar的背景图片
searchBar.backgroundImage = [self imageWithColor:barBackColor size:CGSizeMake(1, 1)];
设置输入框色
首先需要获取UISearchBar的输入框控件
- (UITextField *)getTextField {
if (@available(ios 13.0,*)) {
return [searchBar valueForKey:@"searchField"];
}
else {
return [searchBar valueForKey:@"_searchField"];
}
}
设置输入框背景色
UITextField *searchTextField = [self getTextField];
searchTextField.backgroundColor = textBackColor;