出现时键盘高度会有所不同 [英] keyboard height varies when appearing

查看:83
本文介绍了出现时键盘高度会有所不同的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图通过将底部约束修改为键盘高度来调出键盘出现时的视图.但是返回给我的键盘高度是变化的.

当我点击模拟器中的文本字段时,键盘高度为 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

  1. UIKeyboardFrameEndUserInfoKey gives you the final height according the prefences you have set in your setting.

  2. 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 but UIKeyboardFrameBeginUserInfoKey returns without prediction bar.

Height on toggle in iPhone 5s

这篇关于出现时键盘高度会有所不同的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆