2
This commit is contained in:
124
keyBoard/Class/Search/V/KBSearchBarView.m
Normal file
124
keyBoard/Class/Search/V/KBSearchBarView.m
Normal file
@@ -0,0 +1,124 @@
|
||||
//
|
||||
// KBSearchBarView.m
|
||||
// keyBoard
|
||||
//
|
||||
// 顶部搜索栏封装。
|
||||
//
|
||||
|
||||
#import "KBSearchBarView.h"
|
||||
|
||||
@interface KBSearchBarView () <UITextFieldDelegate>
|
||||
@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);
|
||||
make.height.mas_equalTo(40);
|
||||
}];
|
||||
|
||||
[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);
|
||||
}];
|
||||
}
|
||||
|
||||
#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];
|
||||
_bgView.layer.cornerRadius = 20;
|
||||
_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;
|
||||
}
|
||||
|
||||
@end
|
||||
|
||||
Reference in New Issue
Block a user