码迷,mamicode.com
首页 > 其他好文 > 详细

自定义收索View

时间:2018-01-22 14:14:53      阅读:168      评论:0      收藏:0      [点我收藏+]

标签:elf   select   ^c   inter   nav   ini   textfield   asc   atom   

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

标签:elf   select   ^c   inter   nav   ini   textfield   asc   atom   

原文地址:https://www.cnblogs.com/dujiahong/p/8328557.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!