无法检测到键盘消失吗?

问题描述 投票:0回答:1

我正在尝试检测键盘何时消失,以便降低文本字段。如何修改我的代码来做到这一点?

检测变化的方法:

    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)
}

感谢您的帮助

ios swift nsnotificationcenter uikeyboard
1个回答
3
投票

您可以将它们更新为方法

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
      }

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

© www.soinside.com 2019 - 2024. All rights reserved.