开发者

Hiding keyboard when clear button is pressed in UITextField

Is there any way to hide the keyboard when a clear button of UITextFiel开发者_运维百科d is pressed?


Yes, there is, although I suspect that doing so would violate the Apple Human Interface Guidelines.

To do so, add the following method to your view controller's implementation file. Then make the view controller into your textfield's delegate.

- (BOOL) textFieldShouldClear:(UITextField *)textField{
    [textField resignFirstResponder];   
    return YES;
}

The downside to this approach is if you ever want to prevent the textfield from clearing, your code becomes messy. Instead you might try to define a custom method and then connect it to the valueDidChange method and check for an empty value.

-(IBAction)hideKeyboardFromTextField:(id)sender{
  //TODO: Check if the previous value was longer than one character to differentiate
  //between backspace and clear. 

  //check if the editing caused the box to be empty
  if([[sender value] isEqualToString:@""] || [sender value] == nil)
    [sender resignFirstResponder];
  }
}

The problem here is that you can't easily differentiate between a tap on the clear button and a tap on the delete button when there is one character in the UITextField.

As I said in the beginning of my answer, this is not advisable in the first place and as the answers here have shown, it is not so easy to implement. I don't think it's worth the hassle, considering the difficulty involved and the fact that it doesn't result in optimal user experience.


This code is definitely working for me to hide the key board while clearing out the content of the textfield

- (BOOL)textFieldShouldClear:(UITextField *)textField
{
    textField.text = @"";
    return NO;
}


Yep. Call resignFirstResponder on the text field in the delegate's textFieldShouldClear: method.


In UITextFieldDelegate

- (BOOL)textFieldShouldClear:(UITextField *)textField {
    [textField resignFirstResponder];
    return YES;
}

But there is a problem with this. From the manual, "The text field calls this method in response to the user pressing the built-in clear button. (This button is not shown by default but can be enabled by changing the value in the clearButtonMode property of the text field.) This method is also called when editing begins and the clearsOnBeginEditing property of the text field is set to YES."

Note that, this method is called when editing begins if clearsOnBeginEditing is set to YES. So if you call resignFirstResponder in this method then editing will not begin actually. So you need to set clearsOnBeginEditing to NO. Obviously then the text field won't be cleared when editing begins.

Another IMPORTANT matter not directly related to the question. Hiding the keypad after tapping clear button is not a familiar behavior and Apple does NOT like changing the behavior of standard items. You may get a rejection for this.


Try this code:

[TextField performSelector:@selector(resignFirstResponder) withObject:nil afterDelay:0.1];


For Swift

In your UITextFieldDelegate

func textFieldShouldClear(_ textField: UITextField) -> Bool {
    textField.resignFirstResponse()
    return true
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜