SPSBChooseCitySearchView.m 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. //
  2. // SPSBChooseCitySearchView.m
  3. // 我的社保
  4. //
  5. // Created by shanp on 2021/5/20.
  6. //
  7. #import "SPSBChooseCitySearchView.h"
  8. #import "SPSBUIGeneralHeader.h"
  9. #import "SPSBTextField.h"
  10. @interface SPSBChooseCitySearchView ()<UITextFieldDelegate> {
  11. SPSBTextField *_searchTextField;
  12. UIView *_searchBackgroundView;
  13. }
  14. @end
  15. @implementation SPSBChooseCitySearchView
  16. - (instancetype)init {
  17. self = [super init];
  18. if (!self) return nil;
  19. self.backgroundColor = spsb_EBECED_color();
  20. [self addSearchView];
  21. return self;
  22. }
  23. - (void)addSearchView {
  24. UIView *backgroundView = UIView.new;
  25. backgroundView.backgroundColor = spsb_FFFFFF_color(1.f);
  26. [backgroundView setLayerCornerRadius:5 clipToBounds:false];
  27. [backgroundView setLayerBorderWidth:jxh_onePixe() borderColor:spsb_EEEEEE_color()];
  28. [self addSubview:backgroundView];
  29. UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(changeFirstResponder)];
  30. backgroundView.userInteractionEnabled = true;
  31. [backgroundView addGestureRecognizer:tap];
  32. UIButton *cancelButton = [UIButton convenienceWithFont:spsb_font(16) target:self action:@selector(cancelAction)];
  33. [cancelButton setTitle:@"取消" titleColor:spsb_3296FB_color() state:JXHButtonControlStateNormal];
  34. [cancelButton setContentHorizontalAlignment:UIControlContentHorizontalAlignmentLeft];
  35. [self addSubview:cancelButton];
  36. [backgroundView makeConstraints:^(JXHConstraintMaker *make) {
  37. make.bottom.equalTo(-7);
  38. make.leading.equalTo(8);
  39. make.trailing.equalTo(cancelButton.leading).offset(-10);
  40. make.height.equalTo(30);
  41. }];
  42. [cancelButton makeConstraints:^(JXHConstraintMaker *make) {
  43. make.trailing.equalTo(0);
  44. make.width.equalTo(42);
  45. make.height.equalTo(44);
  46. make.centerY.equalTo(backgroundView.centerY);
  47. }];
  48. _searchBackgroundView = [[UIView alloc] init];
  49. [backgroundView addSubview:_searchBackgroundView];
  50. [_searchBackgroundView makeConstraints:^(JXHConstraintMaker *make) {
  51. make.top.and.bottom.equalTo(0);
  52. make.centerX.equalTo(self->_searchBackgroundView.superview.centerX);
  53. make.leading.greaterThanOrEqualTo(0);
  54. make.trailing.lessThanOrEqualTo(0);
  55. }];
  56. UIImageView *iconImageView = [[UIImageView alloc] initWithImage:jxh_getImage(search_02)];
  57. [_searchBackgroundView addSubview:iconImageView];
  58. [iconImageView setContentCompressionResistancePriority:UILayoutPriorityRequired forAxis:UILayoutConstraintAxisHorizontal];
  59. [iconImageView setContentHuggingPriority:UILayoutPriorityRequired forAxis:UILayoutConstraintAxisHorizontal];
  60. [iconImageView makeConstraints:^(JXHConstraintMaker *make) {
  61. make.leading.equalTo(10);
  62. make.centerY.equalTo(0);
  63. }];
  64. _searchTextField = [SPSBTextField convenienceWithFont:spsb_font(13) placeholder:@"请输入城市名称或按首字母搜索" placeholderColor:spsb_CCCCCC_color() textColor:spsb_333333_color() clearButtonMode:UITextFieldViewModeWhileEditing returnType:UIReturnKeySearch delegate:self];
  65. _searchTextField.enablesReturnKeyAutomatically = true;
  66. [_searchBackgroundView addSubview:_searchTextField];
  67. [_searchTextField makeConstraints:^(JXHConstraintMaker *make) {
  68. make.leading.equalTo(iconImageView.trailing).offset(5);
  69. make.centerY.equalTo(0);
  70. make.trailing.equalTo(0);
  71. }];
  72. [_searchTextField addTarget:self action:@selector(textFieldChanged:) forControlEvents:UIControlEventEditingChanged];
  73. }
  74. #pragma mark - Action
  75. - (void)textFieldChanged:(UITextField *)textField {
  76. [_spsb_delegate searchView:self searchWithCondition:textField.text];
  77. }
  78. - (void)cancelAction {
  79. _searchTextField.text = @"";
  80. [_searchTextField resignFirstResponder];
  81. [_searchBackgroundView remakeConstraints:^(JXHConstraintMaker *make) {
  82. make.top.and.bottom.equalTo(0);
  83. make.centerX.equalTo(self->_searchBackgroundView.superview.centerX);
  84. make.leading.greaterThanOrEqualTo(0);
  85. make.trailing.lessThanOrEqualTo(0);
  86. }];
  87. [UIView animateWithDuration:0.3 animations:^{
  88. [self->_searchBackgroundView.superview layoutSubviews];
  89. }];
  90. [_spsb_delegate searchViewCancelButtonClicked:self];
  91. }
  92. - (void)changeFirstResponder {
  93. [_searchTextField becomeFirstResponder];
  94. }
  95. #pragma mark - UITextFieldDelegate
  96. - (BOOL)textFieldShouldBeginEditing:(UITextField *)textField {
  97. [_searchBackgroundView remakeConstraints:^(JXHConstraintMaker *make) {
  98. make.top.and.bottom.equalTo(0);
  99. make.centerX.equalTo(self->_searchBackgroundView.superview.centerX);
  100. make.leading.equalTo(0);
  101. make.trailing.equalTo(0);
  102. }];
  103. [UIView animateWithDuration:0.3 animations:^{
  104. [self->_searchBackgroundView.superview layoutSubviews];
  105. }];
  106. [_spsb_delegate searchViewBeginEditing:self];
  107. return true;
  108. }
  109. - (BOOL)textFieldShouldReturn:(UITextField *)textField {
  110. [_searchTextField resignFirstResponder];
  111. [_spsb_delegate searchView:self searchWithCondition:textField.text];
  112. return true;
  113. }
  114. @end