Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Tap on UITextField's clear button hides keyboard instead of clearing text

Tags:

In iPhone, I have a view which has a UITextField. When I tap on the clear button of UITextField's the keyboard dismissed instead of clearing the text in the UITextField. On an iPad it is working correctly. What can I do to fix this?

like image 342
Mad Avatar asked Jul 05 '12 04:07

Mad


1 Answers

Just clear the field, resignFirstResponder (if you want to hide keyboard) and return NO/false

Note: set Attributes inspector property of UITextField

Clear Button -> Appears while editing

so it will display the clear button while editing in the text field.

// Objective-C

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

// Swift

func textFieldShouldClear(textField: UITextField) -> Bool {
    textField.text = ""
    textField.resignFirstResponder()
    return false
}
like image 77
Himanshu padia Avatar answered Oct 28 '22 16:10

Himanshu padia