ios - 无法检测到键盘消失?

标签 ios swift nsnotificationcenter uikeyboard

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

检测变化的方法:

    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
      }

使用这些方法你可以改变框架或滚动

关于ios - 无法检测到键盘消失?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58849370/

相关文章:

ios - 解析我的 XML 时出现错误 "Error parsing document! "

ios - Swift 3 音频无法播放

ios - 在 Swift 中创建 PFUser 和对象之间的关系

ios - XCUITest 测试执行顺序

ios - 绘制半径等于手指路径的圆

ios - 更新到 iOS 13 后,UITextField 的建议(电子邮件、电话号码、名字...)不会出现在键盘上方

ios - Auth.auth.currentUser.uid 即使登录有效也无法正常工作

ios - NSNotification中心观察应用是否通过URL Scheme打开

ios - NSNotificationCenter无法正常工作的iOS 7

swift - 从后台获取 NSObjects 时应用程序崩溃