英文:
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
}
然而,在较新型号的手机上,我收到的值将视图放置在键盘顶部之上,如下面的屏幕截图所示。我不愿意使用静态值调整约束,因为这可能会在将来或从型号到型号发生变化。是否有其他人遇到过这个问题并有解决方案?
英文:
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?
答案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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论