Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

iPhone: Possible to dismiss keyboard with UITextField clear button?

I'm wondering if there is a way to have the UITextField clear button 'always visible'

textfield.clearButtonMode = UITextFieldViewModeAlways;

doesn't seem to work .. and if it is possible to dismiss the keyboard using the button?

Thanks in advance.

like image 739
Dan Avatar asked Aug 05 '10 21:08

Dan


People also ask

How do I dismiss the keyboard in iOS?

The first two are standard to the iOS system: Stop editing the note, and the keyboard will disappear. This requires clicking somewhere away from the text. Swipe your finger downwards beginning just above the keyboard, so that you catch the keyboard as you move.

What is UITextField?

An object that displays an editable text area in your interface.

How do I dismiss a TextField?

If you're supporting only iOS 15 and later, you can activate and dismiss the keyboard for a text field by focusing and unfocusing it. In its simplest form, this is done using the @FocusState property wrapper and the focusable() modifier – the first stores a Boolean that tracks whether the second is currently focused.

How do you clear TextField after submit in Swiftui?

The onEditingChange parameter take a closure with a single Bool parameter, if it is true then the TextField has begun to edit the parameter. In your closure check the value of the parameter and clear the TextField variable if it is true.


1 Answers

Like mentioned before it seems apple is setting the textfield focus after you clear the field.

The solution is quite simple. Just clear the field yourself, resignFirstResponder and return NO

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

        return NO;
    }
like image 150
Vinh Nguyen Avatar answered Sep 19 '22 13:09

Vinh Nguyen