How to dismiss keyboard on a UIWebView?

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.

Answers


You can use javascript to take the focus away from the HTML text field using blur:

document.foo.bar.myinput.blur();

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];

UIGestureRecogniser will help to dismiss the keyboard when user clicks on the WebView. Here is the piece of code i used.

let tap = UITapGestureRecognizer(target: self, action: #selector(self.handleTap(_:)))
        tap.delegate = self 
        webView.addGestureRecognizer(tap)

@objc func handleTap(_ sender: UITapGestureRecognizer) { webView?.endEditing(true) }

func gestureRecognizer(_ gestureRecognizer: UIGestureRecognizer, shouldRecognizeSimultaneouslyWith otherGestureRecognizer: UIGestureRecognizer) -> Bool {
  return true
}

Hope the helps.


I'm not so familiar with UIWebView, but normally you would dismiss your keyboard like so...

[textField resignFirstResponder];

By doing that, the keyboard would be dismissed...

To have the keyboard be dismissed when say the user clicks the return button, you would need to implement the delegate method.

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

Apple's documentation has a full list of methods for the UITextFieldDelegate.


Need Your Help

How can I fetch the entire tree in a single query with doctrine?

php tree doctrine

This snippet from official website works as expected:

How to put an image in a custom path

ios iphone path uiimageview uiimage

I am very new in ios programming and here's my problem. I create a path with CGContext (an hexagon) and I want to put an image into this path. Here's the creation path code: