简体   繁体   English

如何在UIWebView上关闭键盘?

[英]How to dismiss keyboard on a UIWebView?

I am loading an HTML page that has a form. 我正在加载一个包含表单的HTML页面。 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. 我希望能够在用户点击GO或者点击HTML页面上的SUBMIT按钮时关闭键盘。

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. 不知道该怎么做。

更简洁的方法,无需知道选择了哪个元素如下:

[webView stringByEvaluatingJavaScriptFromString:@"document.activeElement.blur()"];

javascript方法很聪明,但这更直接:

[webView endEditing:YES];

您可以使用javascript将焦点从HTML文本字段中移开:

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

UIGestureRecogniser will help to dismiss the keyboard when user clicks on the WebView. 当用户点击WebView时,UIGestureRecogniser将帮助关闭键盘。 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) } @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... 我对UIWebView不太熟悉,但通常你会解雇你的键盘......

[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. Apple的文档包含UITextFieldDelegate的完整方法列表。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM