设置背景色
将颜色转为图片
- (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;
本文介绍了如何使用Objective-C代码设置iOS应用中UISearchBar的背景色以及输入框的颜色,包括将颜色转换为UIImage,并将其设置为搜索栏的背景图片,以及如何获取并设置输入框的背景色。
5385

被折叠的 条评论
为什么被折叠?



