iPhone X键盘通知高度不同

4
最近我使用 keyboardWillShowkeyboardWillShow 时遇到了一个奇怪的情况,从 (notification.userInfo?[UIResponder.keyboardFrameBeginUserInfoKey] as? NSValue)?.cgRectValue 获取键盘高度的初始调用返回值为477px,随后所有其他时间都显示值现在是535px,比之前多58px。然而,外观上,键盘并没有任何变化。两个键盘都启用了预测条。
背景信息是,需要键盘高度来抵消包含文本字段的单元格的滚动,我正在比较文本字段的位置,以查看是否在编辑开始时被键盘隐藏。
我的方法理解有什么问题吗?

1
更新:调用 UIResponder.keyboardFrameEndUserInfoKey 似乎在调用 477px 时是一致的。 - Kyle Beard
是的,使用keyboardFrameEndUserInfoKey可以正常工作。谢谢。 - abhimuralidharan
我在iOS 13中也遇到了同样的问题。我的代码如下:let keyboardFrame = notification.userInfo?[UIResponder.keyboardFrameEndUserInfoKey] as! CGRect但是,我得到了不同的frame值。日志:keyboardFrame : ---- (0.0, 550.0, 414.0, 346.0) keyboardFrame : ---- (0.0, 506.0, 414.0, 390.0) - Anita Nagori
你有任何解决方案吗? - Anita Nagori
@Anita,我已经发布了答案,可能有点晚:D - Latenec
谢谢@Latenec,但是是的,现在已经太晚了:), 还是感谢您发布答案:) - Anita Nagori
2个回答

1

希望这可以帮助你,对我来说它很好用

 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是底部约束的输出口


1

我刚遇到了类似的情况,解决方案基本上是这样的:

func keyboardWillShow(_ notification: Notification) {
    guard
        let userInfo = notification.userInfo,
        let animationDuration = userInfo[UIResponder.keyboardAnimationDurationUserInfoKey] as? Double,
        let keyboardEndFrame = (userInfo[UIResponder.keyboardFrameEndUserInfoKey] as? NSValue)?.cgRectValue
    else { return }

    let window = UIApplication.shared.keyWindow
    let bottomPadding = window?.safeAreaInsets.bottom ?? 0.0 // This is the key
    buttonConstraint.constant = keyboardEndFrame.height - bottomPadding
    UIView.animate(withDuration: animationDuration) { [weak self] in
        self?.view.layoutIfNeeded()
    }
}

iPhone X(以及其他具有OLED的设备)具有safeAreaInsets,在您的情况下添加。


谢谢,这个对我有帮助:UIApplication.shared.keyWindow?.safeAreaInsets.bottom ?? 0.0 - ugiflezet

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接