Hiding Keyboard accessorybar in WKWebView

前端 未结 2 1122
鱼传尺愫
鱼传尺愫 2020-12-19 12:21

How can I hide keyboard accessory bar in WKWebview? Although there are some resources for UIWebView, I haven\'t been able to find one for WkWebview on Stackoverflow.

2条回答
  •  情话喂你
    2020-12-19 12:43

    After Swift4

        override func viewWillAppear(_ animated: Bool) {
            super.viewWillAppear(animated)
            NotificationCenter.default.addObserver(self, selector: #selector(keyboardWillDisappear(_:)), name: UIResponder.keyboardWillHideNotification, object: nil)
            NotificationCenter.default.addObserver(self, selector: #selector(keyboardWillAppear(_:)), name: UIResponder.keyboardWillShowNotification, object: nil)
         }
    
        @objc func keyboardWillAppear(_ notification: Notification) {
            //Do something here
            keyboardShowAndHide(true, notification: notification)
        }
    
        @objc func keyboardWillDisappear(_ notification: Notification) {
            //Do something here
            keyboardShowAndHide(false, notification: notification)
        }
        func keyboardShowAndHide(_ open: Bool, notification: Notification) {
          let userInfo = notification.userInfo ?? [:]
            let keyboardFrame = (userInfo[UIResponder.keyboardFrameBeginUserInfoKey] as! NSValue).cgRectValue
          let height = (keyboardFrame.height + 20) * (open ? 1 : -1)
            WKWebView.scrollView.contentInset.bottom += height
            WKWebView.scrollView.scrollIndicatorInsets.bottom += height
        }
    
        override func viewWillDisappear(_ animated: Bool) {
            super.viewWillDisappear(animated)
            //remove keyboard Listener
            NotificationCenter.default.removeObserver(self)
        }
    

提交回复
热议问题