首页 > 解决方案 > iPhone X 键盘通知高度不同

问题描述

我最近在使用时遇到了这种奇怪的情况keyboardWillShowkeyboardWillShow最初调用获取键盘高度的方法(notification.userInfo?[UIResponder.keyboardFrameBeginUserInfoKey] as? NSValue)?.cgRectValue返回一个 477px 的值,然后在所有其他时间它说该值现在是 535px,比它大 58px . 但是,从外观上看,键盘的外观并没有任何变化。两个键盘都启用了预测栏。

作为背景信息,需要键盘高度的目的是偏移表格视图的滚动,其中每个单元格都包含一个文本字段,并且我正在比较文本字段的位置以查看它是否在编辑开始时隐藏在键盘后面。

我在理解它的方法上是否遗漏了一些东西?

标签: iosiphoneswiftuitableviewuitextfield

解决方案


可以帮助你吗它对我有用

 NotificationCenter.default.addObserver(self, selector: #selector(CommentsVC.keyboardWillShow), name: 
 NSNotification.Name.UIKeyboardWillShow, object: nil)
                    NotificationCenter.default.addObserver(self, selector: #selector(CommentsVC.keyboardWillHide), name: NSNotification.Name.UIKeyboardWillHide, object: nil)

@objc func keyboardWillHide(_ notification: NSNotification) {
                UIView.animate(withDuration: 0.3) {
                    self.inputContainerViewBottom.constant = 0


                    self.view.layoutIfNeeded()


                }
            }


@objc func keyboardWillShow(_ notification: NSNotification) {
                print(notification)
                let keyboardFrame = (notification.userInfo?[UIKeyboardFrameEndUserInfoKey] as AnyObject).cgRectValue
                UIView.animate(withDuration: 0.3) {
                    self.inputContainerViewBottom.constant = keyboardFrame!.height

                    self.view.layoutIfNeeded()


                    let flag = self.tableComments.isCellVisible(section: 0, row: 10 - 1)

                    if flag
                    {
                        self.scrollToBottom()
                    }
                    else
                    {

                    }

                }
            }

注意:inputContainerViewBottom 是底部约束的出口


推荐阅读