iOS在新款iPhone上,当键盘可见时,调整视图底部约束常数的偏移量较大。

huangapple go评论75阅读模式
英文:

iOS adjusting bottom constraint constant of view when keyboard visible has a larger offset on newer model iPhones

问题

我正在设置我的键盘将显示通知,获取键盘高度,并将底部约束的常数值设置为如下所示:

通知观察者

NotificationCenter.default.addObserver(self, selector: #selector(keyboardWillShow), name: UIResponder.keyboardWillShowNotification, object: nil)

键盘将显示函数

@objc func keyboardWillShow(notification: NSNotification) {
    guard let keyboardFrame = notification.userInfo?[UIResponder.keyboardFrameEndUserInfoKey] as? NSValue else {
        return
    }

    _keyboardHeight = keyboardFrame.cgRectValue.height
    changeStateContainerBottomConstraint.constant = _keyboardHeight
}

然而,在较新型号的手机上,我收到的值将视图放置在键盘顶部之上,如下面的屏幕截图所示。我不愿意使用静态值调整约束,因为这可能会在将来或从型号到型号发生变化。是否有其他人遇到过这个问题并有解决方案?

iPhone SE 屏幕截图
iOS在新款iPhone上,当键盘可见时,调整视图底部约束常数的偏移量较大。

iPhone 14 Pro 屏幕截图
iOS在新款iPhone上,当键盘可见时,调整视图底部约束常数的偏移量较大。

英文:

I am setting up my keyboard will show notifier, getting the keyboard height, and setting the constant value on the bottom constraint as shown here:

Notification Observer

NotificationCenter.default.addObserver(self, selector: #selector(keyboardWillShow), name: UIResponder.keyboardWillShowNotification, object: nil)

Keyboard Will Show Function

@objc func keyboardWillShow(notification: NSNotification) {
    guard let keyboardFrame = notification.userInfo?[UIResponder.keyboardFrameEndUserInfoKey] as? NSValue else {
        return
    }

    _keyboardHeight = keyboardFrame.cgRectValue.height
    changeStateContainerBottomConstraint.constant = _keyboardHeight
}

However on newer models of phones the value I am getting back is placing the view higher than the top of the keyboard as shown in the screenshots below. I am hesitant to adjust the constraint with a static value as that could change in the future, or from model to model. Has anyone else come across this issue and have a solution?

iPhone SE Screenshot
iOS在新款iPhone上,当键盘可见时,调整视图底部约束常数的偏移量较大。

iPhone 14 Pro Screenshot
iOS在新款iPhone上,当键盘可见时,调整视图底部约束常数的偏移量较大。

答案1

得分: 0

看起来我已经找到了一个可行的解决方案。只需要找到安全区域插图的底部值,并从返回的高度中减去它。以下是keyboardWillShow现在的样子:

@objc func keyboardWillShow(notification: NSNotification) {
    guard let keyboardFrame = notification.userInfo?[UIResponder.keyboardFrameEndUserInfoKey] as? NSValue else {
        return
    }

    _keyboardHeight = keyboardFrame.cgRectValue.height
    let bottom: CGFloat = UIApplication.shared.delegate?.window??.safeAreaInsets.bottom ?? 0
    changeStateContainerBottomConstraint.constant = _keyboardHeight - bottom
}
英文:

It looks like I've found a workable solution. Just needed to find the bottom value of the safe area insets and subtract it from the returned height. Here is what keyboardWillShow looks like now

 @objc func keyboardWillShow(notification: NSNotification) {
    guard let keyboardFrame = notification.userInfo?[UIResponder.keyboardFrameEndUserInfoKey] as? NSValue else {
        return
    }

    _keyboardHeight = keyboardFrame.cgRectValue.height
    let bottom: CGFloat = UIApplication.shared.delegate?.window??.safeAreaInsets.bottom ?? 0
    changeStateContainerBottomConstraint.constant = _keyboardHeight - bottom
}

答案2

得分: -2

我建议您查看这个框架 https://github.com/hackiftekhar/IQKeyboardManager,这是处理这种类型问题的最流行的库。在您的AppDelegate文件中,只需使用几行代码就可以更轻松地使用,例如 IQKeyboardManager.shared.enable = true

英文:

I would suggest you look at this framework https://github.com/hackiftekhar/IQKeyboardManager which is the most popular library to handle this kind of stuff. It's easier to use with a few lines of code like IQKeyboardManager.shared.enable = true in your AppDelegate file.

huangapple
  • 本文由 发表于 2023年5月25日 21:59:44
  • 转载请务必保留本文链接:https://go.coder-hub.com/76333128.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定