出现时键盘高度会有所不同 [英] keyboard height varies when appearing
问题描述
我试图通过将底部约束修改为键盘高度来调出键盘出现时的视图.但是返回给我的键盘高度是变化的.
当我点击模拟器中的文本字段时,键盘高度为 302
.当我尝试打开和关闭软件键盘时,出现键盘时会显示 260
.为什么会这样?
NSNotificationCenter.defaultCenter().addObserver(自己,选择器:#selector(FriendsViewController.keyboardWillShow(_ :)),名称:UIKeyboardWillShowNotification,对象:nil)func keyboardWillShow(通知:NSNotification){打印(出现键盘")警卫让keyboardHeight =(notification.userInfo!作为NSDictionary).objectForKey(UIKeyboardFrameBeginUserInfoKey)?. CGRectValue.size.height else {返回}bottomConstraint.constant = keyboardHeightprint(键盘高度:\(keyboardHeight)")self.view.layoutIfNeeded()}
260
的高度实际上是正确的高度,因为它完美地调整了我的视图.高度为 302
时,我的视图偏移得太远了.
我的视图的布局是.顶部是 UITextField
,下面是 UITableView
.
修改
I am trying to bring my views up when the keyboard appears by modifying the bottom constrain to the keyboard height. But the keyboard height returned to me is varying.
When I tap on a textfield in the simulator, the keyboard height was 302
. When I try to toggle on and off the software keyboard, it shows 260
when the keyboard appears. Why is this happening?
NSNotificationCenter.defaultCenter().addObserver(self, selector: #selector(FriendsViewController.keyboardWillShow(_:)), name: UIKeyboardWillShowNotification, object: nil)
func keyboardWillShow(notification: NSNotification) {
print("Keyboard appearing")
guard let keyboardHeight = (notification.userInfo! as NSDictionary).objectForKey(UIKeyboardFrameBeginUserInfoKey)?.CGRectValue.size.height else {
return
}
bottomConstraint.constant = keyboardHeight
print("keyboard height : \(keyboardHeight)")
self.view.layoutIfNeeded()
}
Height of 260
is actually the correct height, as it adjusted my views perfectly. With a height of 302
my views get offset too far up.
The layout of my view is. UITextField
on the top and followed by a UITableView
below it.
Modified answer of Matt with reason,
He is right you need to use UIKeyboardFrameEndUserInfoKey
instead of UIKeyboardFrameBeginUserInfoKey
because
UIKeyboardFrameEndUserInfoKey
gives you the final height according the prefences you have set in your setting.UIKeyboardFrameEndUserInfoKey
returns you height two times first one is without the language prediction as you can see above the keyboard and next one with predicate if it is activated from the setting butUIKeyboardFrameBeginUserInfoKey
returns without prediction bar.
Height on toggle in iPhone 5s
这篇关于出现时键盘高度会有所不同的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!