// // KBSearchBarView.m // keyBoard // // 顶部搜索栏封装。 // #import "KBSearchBarView.h" @interface KBSearchBarView () @property (nonatomic, strong) UIView *bgView; // 圆角背景 @property (nonatomic, strong, readwrite) UITextField *textField; // 输入框 @property (nonatomic, strong) UIButton *searchButton; // 右侧搜索按钮 @end @implementation KBSearchBarView - (instancetype)initWithFrame:(CGRect)frame { if (self = [super initWithFrame:frame]) { [self setupUI]; } return self; } - (void)setupUI { // 添加子视图 [self addSubview:self.bgView]; [self.bgView addSubview:self.textField]; [self.bgView addSubview:self.searchButton]; // layout - Masonry [self.bgView mas_makeConstraints:^(MASConstraintMaker *make) { make.edges.equalTo(self); // 高度由外部容器决定 }]; [self.searchButton mas_makeConstraints:^(MASConstraintMaker *make) { make.centerY.equalTo(self.bgView); make.right.equalTo(self.bgView).offset(-8); make.width.mas_equalTo(84); make.height.mas_equalTo(32); }]; [self.textField mas_makeConstraints:^(MASConstraintMaker *make) { make.left.equalTo(self.bgView).offset(12); make.centerY.equalTo(self.bgView); make.right.equalTo(self.searchButton.mas_left).offset(-8); make.height.mas_equalTo(30); }]; self.bgView.layer.borderWidth = 1; self.bgView.layer.borderColor = [UIColor colorWithHex:0xE6E6E6].CGColor; } #pragma mark - Action - (void)searchButtonTapped { NSString *kw = self.textField.text ?: @""; if (self.onSearch) { self.onSearch(kw); } } - (BOOL)textFieldShouldReturn:(UITextField *)textField { [textField resignFirstResponder]; if (self.onSearch) { self.onSearch(textField.text ?: @""); } return YES; } - (void)updateKeyword:(NSString *)keyword { self.textField.text = keyword ?: @""; } #pragma mark - Lazy - (UIView *)bgView { if (!_bgView) { _bgView = [[UIView alloc] init]; // 淡灰色背景 + 圆角 _bgView.backgroundColor = [UIColor colorWithWhite:0.95 alpha:1.0]; CGFloat radius = self.bgCornerRadius > 0 ? self.bgCornerRadius : 20.0; _bgView.layer.cornerRadius = radius; _bgView.layer.masksToBounds = YES; } return _bgView; } - (UITextField *)textField { if (!_textField) { _textField = [[UITextField alloc] init]; _textField.delegate = self; _textField.font = [UIFont systemFontOfSize:15]; _textField.textColor = [UIColor colorWithHex:0x1B1F1A]; _textField.clearButtonMode = UITextFieldViewModeWhileEditing; _textField.returnKeyType = UIReturnKeySearch; _textField.placeholder = @"Themes"; // 默认占位 // 左侧占位图标 UIView *pad = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 8, 8)]; _textField.leftView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 6, 1)]; _textField.leftViewMode = UITextFieldViewModeAlways; (void)pad; // 保留变量避免未使用告警 } return _textField; } - (UIButton *)searchButton { if (!_searchButton) { _searchButton = [UIButton buttonWithType:UIButtonTypeSystem]; _searchButton.titleLabel.font = [UIFont systemFontOfSize:16 weight:UIFontWeightSemibold]; [_searchButton setTitle:@"Search" forState:UIControlStateNormal]; // 绿色样式 [_searchButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal]; _searchButton.backgroundColor = [UIColor colorWithRed:0.15 green:0.72 blue:0.62 alpha:1.0]; _searchButton.layer.cornerRadius = 16; _searchButton.layer.masksToBounds = YES; [_searchButton addTarget:self action:@selector(searchButtonTapped) forControlEvents:UIControlEventTouchUpInside]; } return _searchButton; } #pragma mark - Public - (void)setPlaceholder:(NSString *)placeholder { _placeholder = [placeholder copy]; self.textField.placeholder = placeholder; } - (void)setBgCornerRadius:(CGFloat)bgCornerRadius { _bgCornerRadius = bgCornerRadius; // 如果 bgView 已创建,立即更新圆角 if (_bgView) { _bgView.layer.cornerRadius = bgCornerRadius; } } @end