// // SPSBChooseCitySearchView.m // 我的社保 // // Created by shanp on 2021/5/20. // #import "SPSBChooseCitySearchView.h" #import "SPSBUIGeneralHeader.h" #import "SPSBTextField.h" @interface SPSBChooseCitySearchView () { SPSBTextField *_searchTextField; UIView *_searchBackgroundView; } @end @implementation SPSBChooseCitySearchView - (instancetype)init { self = [super init]; if (!self) return nil; self.backgroundColor = spsb_EBECED_color(); [self addSearchView]; return self; } - (void)addSearchView { UIView *backgroundView = UIView.new; backgroundView.backgroundColor = spsb_FFFFFF_color(1.f); [backgroundView setLayerCornerRadius:5 clipToBounds:false]; [backgroundView setLayerBorderWidth:jxh_onePixe() borderColor:spsb_EEEEEE_color()]; [self addSubview:backgroundView]; UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(changeFirstResponder)]; backgroundView.userInteractionEnabled = true; [backgroundView addGestureRecognizer:tap]; UIButton *cancelButton = [UIButton convenienceWithFont:spsb_font(16) target:self action:@selector(cancelAction)]; [cancelButton setTitle:@"取消" titleColor:spsb_3296FB_color() state:JXHButtonControlStateNormal]; [cancelButton setContentHorizontalAlignment:UIControlContentHorizontalAlignmentLeft]; [self addSubview:cancelButton]; [backgroundView makeConstraints:^(JXHConstraintMaker *make) { make.bottom.equalTo(-7); make.leading.equalTo(8); make.trailing.equalTo(cancelButton.leading).offset(-10); make.height.equalTo(30); }]; [cancelButton makeConstraints:^(JXHConstraintMaker *make) { make.trailing.equalTo(0); make.width.equalTo(42); make.height.equalTo(44); make.centerY.equalTo(backgroundView.centerY); }]; _searchBackgroundView = [[UIView alloc] init]; [backgroundView addSubview:_searchBackgroundView]; [_searchBackgroundView makeConstraints:^(JXHConstraintMaker *make) { make.top.and.bottom.equalTo(0); make.centerX.equalTo(self->_searchBackgroundView.superview.centerX); make.leading.greaterThanOrEqualTo(0); make.trailing.lessThanOrEqualTo(0); }]; UIImageView *iconImageView = [[UIImageView alloc] initWithImage:jxh_getImage(search_02)]; [_searchBackgroundView addSubview:iconImageView]; [iconImageView setContentCompressionResistancePriority:UILayoutPriorityRequired forAxis:UILayoutConstraintAxisHorizontal]; [iconImageView setContentHuggingPriority:UILayoutPriorityRequired forAxis:UILayoutConstraintAxisHorizontal]; [iconImageView makeConstraints:^(JXHConstraintMaker *make) { make.leading.equalTo(10); make.centerY.equalTo(0); }]; _searchTextField = [SPSBTextField convenienceWithFont:spsb_font(13) placeholder:@"请输入城市名称或按首字母搜索" placeholderColor:spsb_CCCCCC_color() textColor:spsb_333333_color() clearButtonMode:UITextFieldViewModeWhileEditing returnType:UIReturnKeySearch delegate:self]; _searchTextField.enablesReturnKeyAutomatically = true; [_searchBackgroundView addSubview:_searchTextField]; [_searchTextField makeConstraints:^(JXHConstraintMaker *make) { make.leading.equalTo(iconImageView.trailing).offset(5); make.centerY.equalTo(0); make.trailing.equalTo(0); }]; [_searchTextField addTarget:self action:@selector(textFieldChanged:) forControlEvents:UIControlEventEditingChanged]; } #pragma mark - Action - (void)textFieldChanged:(UITextField *)textField { [_spsb_delegate searchView:self searchWithCondition:textField.text]; } - (void)cancelAction { _searchTextField.text = @""; [_searchTextField resignFirstResponder]; [_searchBackgroundView remakeConstraints:^(JXHConstraintMaker *make) { make.top.and.bottom.equalTo(0); make.centerX.equalTo(self->_searchBackgroundView.superview.centerX); make.leading.greaterThanOrEqualTo(0); make.trailing.lessThanOrEqualTo(0); }]; [UIView animateWithDuration:0.3 animations:^{ [self->_searchBackgroundView.superview layoutSubviews]; }]; [_spsb_delegate searchViewCancelButtonClicked:self]; } - (void)changeFirstResponder { [_searchTextField becomeFirstResponder]; } #pragma mark - UITextFieldDelegate - (BOOL)textFieldShouldBeginEditing:(UITextField *)textField { [_searchBackgroundView remakeConstraints:^(JXHConstraintMaker *make) { make.top.and.bottom.equalTo(0); make.centerX.equalTo(self->_searchBackgroundView.superview.centerX); make.leading.equalTo(0); make.trailing.equalTo(0); }]; [UIView animateWithDuration:0.3 animations:^{ [self->_searchBackgroundView.superview layoutSubviews]; }]; [_spsb_delegate searchViewBeginEditing:self]; return true; } - (BOOL)textFieldShouldReturn:(UITextField *)textField { [_searchTextField resignFirstResponder]; [_spsb_delegate searchView:self searchWithCondition:textField.text]; return true; } @end