我试图通过将底部约束修改为键盘高度来在键盘出现时提升我的 View 。但是返回给我的键盘高度是变化的。
当我点击模拟器中的文本字段时,键盘高度为 302
。当我尝试打开和关闭软件键盘时,它会在键盘出现时显示 260
。为什么会这样?
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()
}
260
的高度实际上是正确的高度,因为它完美地调整了我的 View 。高度为 302
时,我的 View 偏移得太高了。
我的 View 布局是。 UITextField
位于顶部,其后是 UITableView
。
最佳答案
修改后的答案 Matt有道理,
他是对的,你需要使用 UIKeyboardFrameEndUserInfoKey
而不是 UIKeyboardFrameBeginUserInfoKey
因为
UIKeyboardFrameEndUserInfoKey
根据您在设置中设置的首选项为您提供最终高度。UIKeyboardFrameEndUserInfoKey
两次返回您的高度,第一次是没有语言预测,正如您在键盘上方看到的那样,下一次是有谓词的,如果它是从设置中激活的,但是UIKeyboardFrameBeginUserInfoKey
没有预测条返回。
iPhone 5s 中切换高度
关于ios - 出现时键盘高度会发生变化,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42358783/