How to dismiss keyboard on a UIWebView?

前端 未结 5 499
别那么骄傲
别那么骄傲 2021-01-31 21:05

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.

5条回答
  •  慢半拍i
    慢半拍i (楼主)
    2021-01-31 21:56

    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.

提交回复
热议问题