ios - 出现时键盘高度会发生变化

标签 ios swift nsnotifications

我试图通过将底部约束修改为键盘高度来在键盘出现时提升我的 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 因为

  1. UIKeyboardFrameEndUserInfoKey 根据您在设置中设置的首选项为您提供最终高度。

  2. UIKeyboardFrameEndUserInfoKey 两次返回您的高度,第一次是没有语言预测,正如您在键盘上方看到的那样,下一次是有谓词的,如果它是从设置中激活的,但是 UIKeyboardFrameBeginUserInfoKey 没有预测条返回。

iPhone 5s 中切换高度

enter image description here

关于ios - 出现时键盘高度会发生变化,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42358783/

相关文章:

ios - 如何使用 Swift 3 在 Apple map 中将 GeoJSON 绘制为叠加层

ios - 在 deleteItems 后停止 UICollectionView 更新布局

ios - 为什么我的 NSNotification 多次调用它的观察者?

ios - iOS 上的 OpenGL ES 1.1 : No clipping planes?

iphone - iOS 5 中 UILabel 的多色

ios - 具有多个数据模型的多个 Realm

ios - NSNotification 如何工作

iphone - 是否可以通过 nsnotification 将数组作为 obj 传递

ios - 范围不正确

ios - 在 Realm swift4 中使用列表存储嵌套模型对象