无法检测到键盘消失?

Not able to detect keyboard going away?

我正在尝试检测键盘何时消失,以便降低文本字段。我该如何修改我的代码来执行此操作?

检测变化的方法:

    extension NotificationsViewController {

    @objc func keyboardWillChange(notification: Notification) {
        print("something djkshbfkhsalk")

        guard let keyboardRect = (notification.userInfo?[UIResponder.keyboardFrameEndUserInfoKey] as? NSValue)?.cgRectValue else { return }

        if notification.name == UIResponder.keyboardWillShowNotification || notification.name == UIResponder.keyboardWillChangeFrameNotification {
            messegeBox.frame.origin.y = (messegeBox.frame.origin.y-(keyboardRect.height/2))+5
        } else {
            messegeBox.frame.origin.y = messegeBox.frame.origin.y+(keyboardRect.height/2)-5
        }

    }

    @objc func keyboardWillShow(notification: Notification) {  
    }

    @objc func keyboardWillHide(notification: NSNotification) {
        print("fdhsaflkjhdsajkfhdsajkhfjlk")
    }

    @objc func keyboardWillDisapear(notification: NSNotification) {  
    }

    func textFieldShouldReturn(_ textField: UITextField) -> Bool {
        print("fdsafdsafdsafdsafdsaf")
        msgTextField.resignFirstResponder()
        return true
    }

    func scrollViewWillBeginDragging(_ scrollView: UIScrollView) {
        msgTextField.resignFirstResponder()
    }

}

设置第一个发生在viewdidload,第二个是它自己的方法:

        //Listen for keyboard events
    NotificationCenter.default.addObserver(self, selector: #selector(NotificationsViewController.keyboardWillChange(notification:)), name: UIResponder.keyboardWillShowNotification, object: nil)
    NotificationCenter.default.addObserver(self, selector: #selector(NotificationsViewController.keyboardWillChange(notification:)), name: UIResponder.keyboardWillHideNotification, object: nil)
    NotificationCenter.default.addObserver(self, selector: #selector(NotificationsViewController.keyboardWillChange(notification:)), name: UIResponder.keyboardWillChangeFrameNotification, object: nil)

}

deinit {
    //Stop listening for keyboard hide/show events
    NotificationCenter.default.removeObserver(self, name: UIResponder.keyboardWillShowNotification, object: nil)
    NotificationCenter.default.removeObserver(self, name: UIResponder.keyboardWillHideNotification, object: nil)
    NotificationCenter.default.removeObserver(self, name: UIResponder.keyboardWillChangeFrameNotification, object: nil)
}

感谢您的帮助

您可以将这些更新为方法

NotificationCenter.default.addObserver(self, selector: #selector(NotificationsViewController.keyboardWillChange(notification:)), name: UIResponder.keyboardWillShowNotification, object: nil)
NotificationCenter.default.addObserver(self, selector: #selector(NotificationsViewController.keyboardWillChange(notification:)), name: UIResponder.keyboardWillHideNotification, object: nil)

  NotificationCenter.default.addObserver(self, selector: #selector(NotificationsViewController. keyboardWillShow(notification:)), name: UIResponder.keyboardWillShowNotification, object: nil)
  NotificationCenter.default.addObserver(self, selector: #selector(NotificationsViewController. keyboardWillHide(notification:)), name: UIResponder.keyboardWillHideNotification, object: nil)

并且您将在您编写的现有方法中获取事件

     @objc func keyboardWillShow(notification: Notification) {
             //Called before keyboard shows
    //Get the keyboard size like this
    if let keyboardSize = (notification.userInfo? [UIResponder.keyboardFrameEndUserInfoKey] as? NSValue)?.cgRectValue {
        }
      }

      @objc func keyboardWillHide(notification: NSNotification) {
             //Called before keyboard hides
      }

使用这些方法,您可以更改框架或滚动