// // CustomSearchBar.m // zhuxun // // Created by winsoft on 17/2/14. // // #import "CustomSearchBar.h" @implementation CustomSearchBar - (instancetype)init { if (self = [super init]) { [self setBackgroundColor:RGB(255, 255, 255)]; [[[self.subviews objectAtIndex:0].subviews objectAtIndex:0] removeFromSuperview]; [self setSearchTextFieldBackgroundColor:RGB(233, 234, 240)]; } return self; } - (instancetype)initWithFrame:(CGRect)frame { if (self = [super initWithFrame:frame]) { [self setBackgroundColor:RGB(255, 255, 255)]; [[[self.subviews objectAtIndex:0].subviews objectAtIndex:0] removeFromSuperview]; [self setSearchTextFieldBackgroundColor:RGB(233, 234, 240)]; } return self; } - (void)setSearchTextFieldBackgroundColor:(UIColor *)backgroundColor { UITextField *searchTextField = nil; if (isIOS7) { // 经测试, 需要设置barTintColor后, 才能拿到UISearchBarTextField对象 self.barTintColor = [UIColor whiteColor]; for (UIView *subview in self.subviews[0].subviews) { if ([subview isKindOfClass:[UITextField class]]) { searchTextField = subview; break; ; } } } searchTextField.backgroundColor = backgroundColor; // [searchTextField setValue:[UIFont boldSystemFontOfSize:18] forKeyPath:@"_placeholderLabel.font"]; } @end