如何在 swift 中处理软键盘

How to handle Soft Keyboard in swift

我正在开发一个聊天应用程序。在我的应用程序中,我不想在键盘出现或键盘出现在屏幕上时隐藏我的顶部导航栏。我正在使用以下代码来实现我的目标。

 @objc func keyboardWillShow(notification: NSNotification) {
    var  newYpos = CGFloat()
    let rate = notification.userInfo![UIKeyboardAnimationDurationUserInfoKey] as! NSNumber
    if let keyboardSize = (notification.userInfo?[UIKeyboardFrameEndUserInfoKey] as? NSValue)?.cgRectValue {
        self.customView.tableView.contentInset = UIEdgeInsetsMake(0, 0, keyboardSize.height, 0)
        newYpos = keyboardSize.height
        //   print(newYpos)
        self.tap.isEnabled = true
    }
    if self.yPosAfterFirstNotif == 0.0{
        self.yPosAfterFirstNotif = newYpos
        UIView.animate(withDuration: rate.doubleValue, animations: {
            self.customView.bottomC.constant = (-self.yPosAfterFirstNotif)
        })
    }
    self.customView.tableView.contentInset = UIEdgeInsetsMake(0, 0, newYpos, 0)
    //scrollToBottom()
    self.customView.tableView.scrollToBottomm()
    self.customView.tableView.contentInset = UIEdgeInsetsMake(0, 0, 0, 0)
}

这就是我隐藏键盘的方式

  @objc func keyboardWillHide(notification: NSNotification) {
          var  newYpos = CGFloat()
      let rate = notification.userInfo![UIKeyboardAnimationDurationUserInfoKey] as! NSNumber
            if let keyboardSize = (notification.userInfo?[UIKeyboardFrameBeginUserInfoKey] as? NSValue)?.cgRectValue
            {
                 newYpos = keyboardSize.height
                 self.yPosAfterFirstNotif = newYpos
                self.customView.tableView.contentInset = UIEdgeInsetsMake(0, 0, newYpos, 0)
            }
            UIView.animate(withDuration: rate.doubleValue, animations: {
                self.customView.bottomC.constant = (+self.yPosAfterFirstNotif)
            })
            //  self.constraintCommentViewBottom.constant = 0
            self.yPosAfterFirstNotif = 0.0
            self.customView.tableView.contentInset = UIEdgeInsetsMake(0, 0, newYpos, 0)

        self.tap.isEnabled = false


        if self.customView.ChatTextField.text.characters.count == 0
        {
            self.customView.sendButton.isHidden = true
            self.customView.recordButton.isHidden = false
            self.customView.cameraButton.isHidden = false
            self.customView.textFieldTrailling.constant = 44
        }




    }

before presenting keyboard

after presenting keyboard

after dismissing keyboard

需要帮忙 谢谢

请尝试将以下代码添加到键盘 hide/show 上的 hide/show 导航栏

您还可以根据键盘大小以相同的方法管理滚动。

更新答案:: 您可以将约束设置为视图底部包含文本字段并在键盘上更新它 show\hide。这个对我有用。

override func viewDidLoad() {
    super.viewDidLoad()
    NotificationCenter.default.addObserver(self, selector: #selector(ViewController.keyboardWillShow), name: UIResponder.keyboardWillShowNotification, object: nil)
    NotificationCenter.default.addObserver(self, selector: #selector(ViewController.keyboardWillHide), name: UIResponder.keyboardWillHideNotification, object: nil)

}

@objc func keyboardWillShow(notification: Notification) {
     guard let userInfo = (sender as Notification).userInfo, let value = userInfo[UIResponder.keyboardFrameEndUserInfoKey] as? NSValue else { return }
    let newHeight: CGFloat
    if #available(iOS 11.0, *) {
        newHeight = value.cgRectValue.height - view.safeAreaInsets.bottom
    } else {
        newHeight = value.cgRectValue.height
    }
    self.chatViewBottomConstraints.constant = newHeight
    DispatchQueue.main.async {
        self.tableView.scrollToBottom()
    }

}

@objc func keyboardWillHide(notification: Notification) {
 DispatchQueue.main.async {
        self.chatViewBottomConstraints.constant  = 0.0
        self.tableView.scrollToBottom()
    }
}

我认为 keyboardWillHide 中的 keyboardSize 不正确,请尝试使用 UIKeyboardFrameEndUserInfokey 而不是 UIKeyboardFrameBeginUserInfoKey