//更改UISearchBar 的 iOS7 中的取消按钮色调颜色
NSMutableDictionary* dic = [NSMutableDictionary dictionary];
dic[NSForegroundColorAttributeName] = FXQColor(98, 98, 100);
[[UIBarButtonItem appearanceWhenContainedIn:[UISearchBar class], nil]setTitleTextAttributes:dic forState:UIControlStateNormal];
for (UIView *view in _searchBar.subviews) {
// for before iOS7.0
if ([view isKindOfClass:NSClassFromString(@"UISearchBarBackground")]) {
[view removeFromSuperview];
break;
}
// for later iOS7.0(include)
if ([view isKindOfClass:NSClassFromString(@"UIView")] && view.subviews.count > 0) {
[[view.subviews objectAtIndex:0] removeFromSuperview];
break;
}
}
//FXQColor(243, 243, 243)
[self setSearchTextFieldBackgroundColor:[UIColor redColor] searchBar:_searchBar];
/** 修改searchbar的背景色 */
- (void)setSearchTextFieldBackgroundColor:(UIColor *)backgroundColor searchBar:(UISearchBar *)search
{
UIView *searchTextField = nil;
if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 7.0) {
// 经测试, 需要设置barTintColor后, 才能拿到UISearchBarTextField对象
_searchBar.barTintColor = [UIColor whiteColor];
searchTextField = [[[_searchBar.subviews firstObject] subviews] lastObject];
} else { // iOS6以下版本searchBar内部子视图的结构不一样
for (UIView *subView in _searchBar.subviews) {
if ([subView isKindOfClass:NSClassFromString(@"UISearchBarTextField")]) {
searchTextField = subView;
}
}
}
searchTextField.backgroundColor = backgroundColor;
}
//取消searchbar背景色
- (UIImage *)imageWithColor:(UIColor *)color size:(CGSize)size
{
CGRect rect = CGRectMake(0, 0, size.width, size.height);
UIGraphicsBeginImageContext(rect.size);
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextSetFillColorWithColor(context, [color CGColor]);
CGContextFillRect(context, rect);
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return image;
}