Files
yinmeng-ios/xplan-ios/Main/Home/View/SubViews/XPHomeSearchNavView.m

179 lines
5.9 KiB
Mathematica
Raw Normal View History

2021-11-29 21:40:11 +08:00
//
// XPHomeSearchNavView.m
// xplan-ios
//
// Created by on 2021/11/29.
//
#import "XPHomeSearchNavView.h"
///Third
#import <Masonry/Masonry.h>
///Tool
#import "ThemeColor.h"
#import "XPMacro.h"
#import "UIButton+EnlargeTouchArea.h"
@interface XPHomeSearchNavView ()
///
@property (nonatomic,strong) UIButton *backButton;
///
@property (nonatomic,strong) UIView * inputBackView;
///logo
@property (nonatomic,strong) UIImageView *searchImageView;
///
@property (nonatomic,strong) UITextField *searchTextField;
///
@property (nonatomic,strong) UIButton *cancleButton;
@end
@implementation XPHomeSearchNavView
2022-09-06 11:41:12 +08:00
- (void)dealloc {
[[NSNotificationCenter defaultCenter] removeObserver:self];
}
2021-11-29 21:40:11 +08:00
- (instancetype)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
[self initSubViews];
[self initSubViewConstraints];
2022-09-06 11:41:12 +08:00
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(textFieldChange:) name:UITextFieldTextDidChangeNotification object:nil];
2021-11-29 21:40:11 +08:00
}
return self;
}
#pragma mark - Private Method
- (void)initSubViews {
[self addSubview:self.backButton];
[self addSubview:self.inputBackView];
[self addSubview:self.cancleButton];
[self.inputBackView addSubview:self.searchImageView];
[self.inputBackView addSubview:self.searchTextField];
}
- (void)initSubViewConstraints {
[self.backButton mas_makeConstraints:^(MASConstraintMaker *make) {
make.size.mas_equalTo(CGSizeMake(22, 22));
make.left.mas_equalTo(self).offset(8);
make.centerY.mas_equalTo(self.inputBackView);
}];
[self.inputBackView mas_makeConstraints:^(MASConstraintMaker *make) {
make.height.mas_equalTo(30);
make.left.mas_equalTo(self.backButton.mas_right).offset(5);
make.right.mas_equalTo(self.cancleButton.mas_left).offset(-13);
make.top.mas_equalTo(self).offset(statusbarHeight + 10);
}];
[self.cancleButton mas_makeConstraints:^(MASConstraintMaker *make) {
make.right.mas_equalTo(self).offset(-15);
make.centerY.mas_equalTo(self.inputBackView);
make.height.mas_equalTo(30);
}];
[self.searchImageView mas_makeConstraints:^(MASConstraintMaker *make) {
make.size.mas_equalTo(CGSizeMake(18, 18));
make.centerY.mas_equalTo(self.inputBackView);
make.left.mas_equalTo(self.inputBackView).offset(9);
}];
[self.searchTextField mas_makeConstraints:^(MASConstraintMaker *make) {
make.left.mas_equalTo(self.searchImageView.mas_right);
make.right.top.bottom.mas_equalTo(self.inputBackView);
}];
}
#pragma mark - Event Response
- (void)cancleButtonAction:(UIButton *)sender {
if (self.delegate && [self.delegate respondsToSelector:@selector(xPHomeSearchNavView:didClickSearch:)]) {
[self.delegate xPHomeSearchNavView:self didClickSearch:sender];
}
}
- (void)backButtonAction:(UIButton *)sender {
if (self.delegate && [self.delegate respondsToSelector:@selector(xPHomeSearchNavView:didClickBack:)]) {
[self.delegate xPHomeSearchNavView:self didClickBack:sender];
}
}
2022-09-06 11:41:12 +08:00
- (void)textFieldChange:(NSNotification *)notification {
UITextField *textField = (UITextField *)notification.object;
NSString *searchStr = textField.text;
if (searchStr.length == 0) {
if (self.delegate && [self.delegate respondsToSelector:@selector(xPHomeSearchNavViewClearTextField:)]) {
[self.delegate xPHomeSearchNavViewClearTextField:self];
}
}
}
2021-11-29 21:40:11 +08:00
#pragma mark - Getters And Setters
- (UIButton *)backButton {
if (!_backButton) {
_backButton = [UIButton buttonWithType:UIButtonTypeCustom];
2021-12-17 19:31:26 +08:00
[_backButton setImage:[UIImage imageNamed:@"common_nav_back"] forState:UIControlStateNormal];
[_backButton setImage:[UIImage imageNamed:@"common_nav_back"] forState:UIControlStateSelected];
2021-11-29 21:40:11 +08:00
[_backButton addTarget:self action:@selector(backButtonAction:) forControlEvents:UIControlEventTouchUpInside];
[_backButton setEnlargeEdgeWithTop:10 right:5 bottom:10 left:8];
}
return _backButton;
}
- (UIView *)inputBackView {
if (!_inputBackView) {
_inputBackView = [[UIView alloc] init];
_inputBackView.backgroundColor = [ThemeColor appCellBackgroundColor];
_inputBackView.layer.masksToBounds = YES;
_inputBackView.layer.cornerRadius = 15;
}
return _inputBackView;
}
- (UIImageView *)searchImageView {
if (!_searchImageView) {
_searchImageView = [[UIImageView alloc] init];
_searchImageView.userInteractionEnabled = YES;
2022-09-22 14:08:26 +08:00
_searchImageView.image = [UIImage imageNamed:@"home_nav_search"];
2021-11-29 21:40:11 +08:00
}
return _searchImageView;
}
- (UITextField *)searchTextField {
if (!_searchTextField) {
_searchTextField = [[UITextField alloc] init];
_searchTextField.layer.cornerRadius = 15;
_searchTextField.layer.masksToBounds = YES;
_searchTextField.tintColor = [ThemeColor secondTextColor];
_searchTextField.textColor = [ThemeColor mainTextColor];
_searchTextField.backgroundColor = [UIColor clearColor];
_searchTextField.font = [UIFont systemFontOfSize:13];
2023-01-05 16:33:37 +08:00
NSString *placeholder = [NSString stringWithFormat:@"搜索昵称/ID/房间名"];
2021-11-29 21:40:11 +08:00
_searchTextField.attributedPlaceholder = [[NSAttributedString alloc] initWithString:placeholder attributes:@{NSFontAttributeName : [UIFont systemFontOfSize:13], NSForegroundColorAttributeName : [ThemeColor secondTextColor]}];
_searchTextField.clearButtonMode = UITextFieldViewModeWhileEditing;
_searchTextField.returnKeyType = UIReturnKeySearch;
_searchTextField.enablesReturnKeyAutomatically = YES;
}
return _searchTextField;
}
- (UIButton *)cancleButton {
if (!_cancleButton) {
_cancleButton = [UIButton buttonWithType:UIButtonTypeCustom];
[_cancleButton setTitle:@"搜索" forState:UIControlStateNormal];
[_cancleButton setTitleColor:[ThemeColor secondTextColor] forState:UIControlStateNormal];
_cancleButton.titleLabel.font = [UIFont fontWithName:@"PingFang-SC-Medium" size:16];
[_cancleButton addTarget:self action:@selector(cancleButtonAction:) forControlEvents:UIControlEventTouchUpInside];
[_cancleButton setEnlargeEdgeWithTop:10 right:10 bottom:10 left:0];
}
return _cancleButton;
}
@end