CustomSearchBar.m 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. //
  2. // CustomSearchBar.m
  3. // zhuxun
  4. //
  5. // Created by winsoft on 17/2/14.
  6. //
  7. //
  8. #import "CustomSearchBar.h"
  9. @implementation CustomSearchBar
  10. - (instancetype)init
  11. {
  12. if (self = [super init]) {
  13. [self setBackgroundColor:RGB(255, 255, 255)];
  14. [[[self.subviews objectAtIndex:0].subviews objectAtIndex:0] removeFromSuperview];
  15. [self setSearchTextFieldBackgroundColor:RGB(233, 234, 240)];
  16. }
  17. return self;
  18. }
  19. - (instancetype)initWithFrame:(CGRect)frame
  20. {
  21. if (self = [super initWithFrame:frame]) {
  22. [self setBackgroundColor:RGB(255, 255, 255)];
  23. [[[self.subviews objectAtIndex:0].subviews objectAtIndex:0] removeFromSuperview];
  24. [self setSearchTextFieldBackgroundColor:RGB(233, 234, 240)];
  25. }
  26. return self;
  27. }
  28. - (void)setSearchTextFieldBackgroundColor:(UIColor *)backgroundColor
  29. {
  30. UITextField *searchTextField = nil;
  31. if (isIOS7) {
  32. // 经测试, 需要设置barTintColor后, 才能拿到UISearchBarTextField对象
  33. self.barTintColor = [UIColor whiteColor];
  34. for (UIView *subview in self.subviews[0].subviews) {
  35. if ([subview isKindOfClass:[UITextField class]]) {
  36. searchTextField = subview;
  37. break;
  38. ;
  39. }
  40. }
  41. }
  42. searchTextField.backgroundColor = backgroundColor;
  43. // [searchTextField setValue:[UIFont boldSystemFontOfSize:18] forKeyPath:@"_placeholderLabel.font"];
  44. }
  45. @end