1 .h文件
@interface SearchNavView : UIView
@property (nonatomic, copy) void(^cancleBtnBlock)(void);
@property (nonatomic, copy) void(^textFiledEditingBlock)(NSString *contentStr);
@end
2 .m文件
#import "SearchNavView.h"
@interface SearchNavView()<UITextFieldDelegate>
@property (nonatomic, strong) UITextField *searchTextFiled;
@property (nonatomic, strong) UIButton *cancleBtn;
@end @implementation SearchNavView - (instancetype)initWithFrame:(CGRect)frame
{
if (self = [super initWithFrame:frame]) {
[self addSubview:self.searchTextFiled];
[self addSubview:self.cancleBtn];
_searchTextFiled.delegate = self;
}
return self;
}
- (void)layoutSubviews
{
[super layoutSubviews];
__weak typeof(self)weakself = self;
[self.searchTextFiled mas_makeConstraints:^(MASConstraintMaker *make) {
make.centerY.equalTo(weakself.mas_centerY);
make.left.equalTo(weakself.mas_left).offset( / WIDTH_6S_SCALE);
make.width.mas_equalTo( / WIDTH_6S_SCALE);
make.height.mas_equalTo( / HEIGHT_6S_SCALE);
}];
_searchTextFiled.layer.cornerRadius = ;
_searchTextFiled.layer.masksToBounds = YES;
[self.cancleBtn mas_makeConstraints:^(MASConstraintMaker *make) {
make.centerY.equalTo(weakself.mas_centerY);
make.right.equalTo(weakself.mas_right).offset(- / WIDTH_6S_SCALE);
make.width.mas_equalTo( / WIDTH_6S_SCALE);
make.height.mas_equalTo( / HEIGHT_6S_SCALE);
}];
}
#pragma mark -UITextFieldDelegate
- (void)textFieldDidEndEditing:(UITextField *)textField
{
if (self.textFiledEditingBlock) {
self.textFiledEditingBlock(self.searchTextFiled.text);
}
}
#pragma mark - event
- (void)cancleBtnAction
{
if (self.cancleBtnBlock) {
self.cancleBtnBlock();
}
}
#pragma mark - init
- (UITextField *)searchTextFiled
{
if (!_searchTextFiled) {
_searchTextFiled = [[UITextField alloc]init];
_searchTextFiled.backgroundColor = getColor(bgColor);
_searchTextFiled.font = DEF_FontSize_14;
_searchTextFiled.textColor = getColor(textColor);
_searchTextFiled.textAlignment = NSTextAlignmentCenter;
_searchTextFiled.placeholder = @"请输入搜索关键词";
}
return _searchTextFiled;
}
- (UIButton *)cancleBtn
{
if (!_cancleBtn) {
_cancleBtn = [[UIButton alloc]init];
_cancleBtn.backgroundColor = [UIColor clearColor];
[_cancleBtn setTitleColor:getColor(textColor) forState:UIControlStateNormal];
_cancleBtn.titleLabel.font = DEF_FontSize_14;
[_cancleBtn setTitle:@"取消" forState:UIControlStateNormal];
[_cancleBtn addTarget:self action:@selector(cancleBtnAction) forControlEvents:UIControlEventTouchUpInside];
}
return _cancleBtn;
}
@end