根据用户是否输入和是否文本框内容为空来决定显示按钮颜色。
也可以实现按钮是否可以点击。
-(UIView *)inputTextFiled{ if (!_inputTextFiled) { _inputTextFiled = [self createTextField]; _inputTextFiled.keyboardType = UIKeyboardTypeDefault; _inputTextFiled.font = [UIFont systemFontOfSize:12]; _inputTextFiled.backgroundColor = BGColorHex(F7F7F7); _inputTextFiled.frame = CGRectMake(10, 0, FULL_WIDTH-10*4 - 30 -60 -10, 30); NSMutableAttributedString *placeholderString = [[NSMutableAttributedString alloc] initWithString:@"请文明发言以免禁言~" attributes:@{NSForegroundColorAttributeName:RGB(151, 151, 151)}]; _inputTextFiled.attributedPlaceholder = placeholderString; _inputTextFiled.delegate = self; _inputTextFiled.userInteractionEnabled = YES; _inputTextFiled.clearButtonMode = UITextFieldViewModeAlways; //解决ios9及以下版本,点击键盘上方工具栏文字,按钮不变色问题 @weakify(self); [[_inputTextFiled rac_signalForControlEvents:UIControlEventEditingChanged] subscribeNext:^(id x) { @strongify(self); if(isCommonUnitEmptyString(self.inputTextFiled.text)) { self.sendButton.backgroundColor = RGB(196, 196, 196); } else { self.sendButton.backgroundColor = BGColorHex(F74490); } }]; } return _inputTextFiled; } - (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string { if(textField == self.inputTextFiled) { if(isEmptyString(string)) { // return YES; if(range.length < self.inputTextFiled.text.length) { return YES; } else { _sendButton.backgroundColor = RGB(196, 196, 196); return YES; } } else { if(messageMaxLength < self.inputTextFiled.text.length + string.length) { return NO; } else { _sendButton.backgroundColor = BGColorHex(F74490); return YES; } } return YES; } else { return YES; } } - (BOOL)textFieldShouldClear:(UITextField *)textField { //删除事件 self.sendButton.backgroundColor = RGB(196, 196, 196); return YES; }