I am loading an HTML page that has a form. I would like to be able to dismiss the keyboard when the user clicks on GO or if he clicks on the SUBMIT button on the HTML page.
If the user decides he doesn't want to fill out the form, I also need a way to dismiss the keyboard.
Not sure how to do this.
Android devices have a solution; press the physical back button (provided on some mobile phones) or the soft key back button, and it closes the keyboard.
Via Tap Gesture This is the quickest way to implement keyboard dismissal. Just set a Tap gesture on the main View and hook that gesture with a function which calls view. endEditing . Causes the view (or one of its embedded text fields) to resign the first responder status.
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.
A more concise way without needing to know what element is selected is the following:
[webView stringByEvaluatingJavaScriptFromString:@"document.activeElement.blur()"];
The javascript approach is clever, but this is more direct:
[webView endEditing:YES];
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With