EMSearchBar.m 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. //
  2. // EMSearchBar.m
  3. // ChatDemo-UI3.0
  4. //
  5. // Created by XieYajie on 2019/1/16.
  6. // Copyright © 2019 XieYajie. All rights reserved.
  7. //
  8. #import "EMSearchBar.h"
  9. @interface EMSearchBar()<UITextFieldDelegate>
  10. @property (nonatomic, strong) UIButton *cancelButton;
  11. @end
  12. @implementation EMSearchBar
  13. - (instancetype)init
  14. {
  15. self = [super init];
  16. if (self) {
  17. [self _setupSubviews];
  18. [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(textFieldTextDidChange) name:UITextFieldTextDidChangeNotification object:nil];
  19. }
  20. return self;
  21. }
  22. - (void)dealloc
  23. {
  24. [[NSNotificationCenter defaultCenter] removeObserver:self];
  25. }
  26. #pragma mark - Subviews
  27. - (void)_setupSubviews
  28. {
  29. self.backgroundColor = [UIColor whiteColor];
  30. self.textField = [[UITextField alloc] init];
  31. self.textField.delegate = self;
  32. self.textField.backgroundColor = kColor_LightGray;
  33. self.textField.font = [UIFont systemFontOfSize:16];
  34. self.textField.placeholder = @"搜索";
  35. self.textField.clearButtonMode = UITextFieldViewModeWhileEditing;
  36. self.textField.leftViewMode = UITextFieldViewModeAlways;
  37. self.textField.returnKeyType = UIReturnKeySearch;
  38. self.textField.layer.cornerRadius = 8;
  39. [self addSubview:self.textField];
  40. [self.textField mas_makeConstraints:^(MASConstraintMaker *make) {
  41. make.centerY.equalTo(self);
  42. make.left.equalTo(self).offset(15);
  43. make.right.equalTo(self).offset(-15);
  44. make.height.equalTo(@35);
  45. }];
  46. UIImageView *leftView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 30, 15)];
  47. leftView.contentMode = UIViewContentModeScaleAspectFit;
  48. leftView.image = [UIImage imageNamed:@"search_gray"];
  49. self.textField.leftView = leftView;
  50. self.cancelButton = [[UIButton alloc] init];
  51. self.cancelButton.titleLabel.font = [UIFont systemFontOfSize:16];
  52. [self.cancelButton setTitle:@"取消" forState:UIControlStateNormal];
  53. [self.cancelButton setTitleColor:kColor_Blue forState:UIControlStateNormal];
  54. [self.cancelButton addTarget:self action:@selector(searchCancelButtonClicked) forControlEvents:UIControlEventTouchUpInside];
  55. }
  56. #pragma mark - UITextFieldDelegate
  57. - (BOOL)textFieldShouldBeginEditing:(UITextField *)textField
  58. {
  59. [self addSubview:self.cancelButton];
  60. [self.cancelButton mas_makeConstraints:^(MASConstraintMaker *make) {
  61. make.centerY.equalTo(self);
  62. make.right.equalTo(self).offset(-5);
  63. make.width.equalTo(@50);
  64. make.height.equalTo(self);
  65. }];
  66. [self.textField mas_updateConstraints:^(MASConstraintMaker *make) {
  67. make.right.equalTo(self).offset(-65);
  68. }];
  69. if (self.delegate && [self.delegate respondsToSelector:@selector(searchBarShouldBeginEditing:)]) {
  70. [self.delegate searchBarShouldBeginEditing:self];
  71. }
  72. return YES;
  73. }
  74. - (BOOL)textFieldShouldReturn:(UITextField *)textField
  75. {
  76. [textField resignFirstResponder];
  77. if (self.delegate && [self.delegate respondsToSelector:@selector(searchBarSearchButtonClicked:)]) {
  78. [self.delegate searchBarSearchButtonClicked:textField.text];
  79. }
  80. return YES;
  81. }
  82. #pragma mark - Action
  83. - (void)textFieldTextDidChange
  84. {
  85. if (self.delegate && [self.delegate respondsToSelector:@selector(searchTextDidChangeWithString:)]) {
  86. [self.delegate searchTextDidChangeWithString:self.textField.text];
  87. }
  88. }
  89. - (void)searchCancelButtonClicked
  90. {
  91. [self.cancelButton removeFromSuperview];
  92. [self.textField resignFirstResponder];
  93. self.textField.text = nil;
  94. [self.textField mas_updateConstraints:^(MASConstraintMaker *make) {
  95. make.right.equalTo(self).offset(-15);
  96. }];
  97. if (self.delegate && [self.delegate respondsToSelector:@selector(searchBarCancelButtonAction:)]) {
  98. [self.delegate searchBarCancelButtonAction:self];
  99. }
  100. }
  101. @end