【问题标题】:iPhone X Keyboard Notification Height DiffersiPhone X 键盘通知高度不同
【发布时间】:2019-05-23 22:46:18
【问题描述】:

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

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

我在理解它的方法上是否遗漏了什么?

【问题讨论】:

  • 更新:调用 UIResponder.keyboardFrameEndUserInfoKey 似乎与调用 477px 一致。
  • 是的,它适用于 keyboardFrameEndUserInfoKey。谢谢。
  • 我在 ios 13 中也遇到了同样的问题 我得到了这样的框架:let keyboardFrame = notification.userInfo?[UIResponder.keyboardFrameEndUserInfoKey] as! CGRect,我得到了不同的帧日志keyboardFrame值:----(0.0、550.0、414.0、346.0)keyboardFrame:----(0.0、506.0、414.0、390.0)
  • 你有什么解决办法吗?
  • @Anita 我已经发布了答案,可能有点晚了:D

标签: ios iphone swift uitableview uitextfield


【解决方案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
【解决方案2】:

希望这对你有帮助,它对我有用

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

【讨论】:

    猜你喜欢
    • 2018-03-07
    • 2011-10-29
    • 2015-03-05
    • 2015-12-04
    • 2018-01-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-07-02
    相关资源
    最近更新 更多