自定义收索View

Posted 宁静暖风

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了自定义收索View相关的知识,希望对你有一定的参考价值。

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(10 / WIDTH_6S_SCALE);
        make.width.mas_equalTo(280 / WIDTH_6S_SCALE);
        make.height.mas_equalTo(30 / HEIGHT_6S_SCALE);
    }];
    _searchTextFiled.layer.cornerRadius = 15;
    _searchTextFiled.layer.masksToBounds = YES;
    [self.cancleBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(weakself.mas_centerY);
        make.right.equalTo(weakself.mas_right).offset(-10 / WIDTH_6S_SCALE);
        make.width.mas_equalTo(50 / WIDTH_6S_SCALE);
        make.height.mas_equalTo(30 / 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

 

以上是关于自定义收索View的主要内容,如果未能解决你的问题,请参考以下文章

如何在片段 xml 中使用自定义组件?

VSCode自定义代码片段——CSS选择器

VSCode自定义代码片段6——CSS选择器

VSCode自定义代码片段(vue主模板)

VSCode自定义代码片段——声明函数

VSCode自定义代码片段——.vue文件的模板