Swift 4显示/隐藏键盘视图

use*_*331 0 uikeyboard nsnotificationcenter ios swift

我试图在我的键盘上实现一个显示/隐藏,但问题是当我显示我的键盘它超过我的视图,更重要的是它超过我的文本字段和按钮,所以我可以输入,我只是不能按我的按钮.

以下是键盘前的屏幕截图:

在此输入图像描述

这是键盘出现的时间:

在此输入图像描述

如你所见,我看不到文字字段和按钮.

这是我的代码 viewDidLoad

NotificationCenter.default.addObserver(self, selector: #selector(CommentsController.keyboardWillShow), name: NSNotification.Name.UIKeyboardWillShow, object: nil)
NotificationCenter.default.addObserver(self, selector: #selector(CommentsController.keyboardWillHide), name: NSNotification.Name.UIKeyboardWillHide, object: nil)
Run Code Online (Sandbox Code Playgroud)

以下是方法:

@objc func keyboardWillShow(notification: NSNotification) {
    if let keyboardSize = (notification.userInfo?[UIKeyboardFrameBeginUserInfoKey] as? NSValue)?.cgRectValue {
          if self.view.frame.origin.y == 0{
              self.view.frame.origin.y -= keyboardSize.height
          }
     }
 }

@objc func keyboardWillHide(notification: NSNotification) {
     if let keyboardSize = (notification.userInfo?[UIKeyboardFrameBeginUserInfoKey] as? NSValue)?.cgRectValue {
          if self.view.frame.origin.y != 0{
              self.view.frame.origin.y += keyboardSize.height
          }
     }
 }
Run Code Online (Sandbox Code Playgroud)

为什么我的键盘会超出我的视线?以及如何解决?

Sh_*_*han 5

您需要更换UIKeyboardFrameBeginUserInfoKeyUIKeyboardFrameEndUserInfoKey