简体   繁体   English

获得键盘的高度不适用于IOS 11 beta

[英]Get the height of keyboard doesn't work on IOS 11 beta

I have the following code which worked on IOS 10, but now it doesn't work anymore when running on IOS 11 beta. 我有以下代码可用于IOS 10,但现在它在IOS 11 beta上运行时不再起作用。

if let userInfo = notification.userInfo {
    if let keyboardSize = (userInfo[UIKeyboardFrameBeginUserInfoKey] as? NSValue)?.cgRectValue {
        print(keyboardSize)
    }
}

This is what I get when I print the size: 这是我打印尺寸时得到的:

(0.0, 0.0, 0.0, 0.0)
(0.0, 736.0, 414.0, 0.0)

Anyone knows why this has stopped working ? 谁知道为什么这已停止工作? Or if I have any other alternatives to get the keyboard size ? 或者,如果我有其他选择来获得键盘尺寸?

Use UIKeyboardFrameEndUserInfoKey instead of UIKeyboardFrameBeginUserInfoKey 使用UIKeyboardFrameEndUserInfoKey代替UIKeyboardFrameBeginUserInfoKey

So changing your code to the following will fix your issue: 因此,将代码更改为以下内容将解决您的问题:

if let userInfo = notification.userInfo {
    if let keyboardSize = (userInfo[UIKeyboardFrameEndUserInfoKey] as? NSValue)?.cgRectValue {
        print(keyboardSize)
    }
}

I have the same issue. 我有同样的问题。 The answer of Doug Amos is right. Doug Amos的答案是对的。 I just want to make it clearer. 我只是想让它更清楚。 Here is my code: 这是我的代码:

@objc func keyboardWillShow(notification:NSNotification){

        var userInfo = notification.userInfo!
        var keyboardFrame:CGRect = (userInfo[UIKeyboardFrameBeginUserInfoKey] as! NSValue).cgRectValue
        if keyboardFrame.size.height <= 0 { // to fix bug on iOS 11
            keyboardFrame = (userInfo[UIKeyboardFrameEndUserInfoKey] as! NSValue).cgRectValue
        }
        keyboardFrame = self.view.convert(keyboardFrame, from: nil)
    }

I used this code in my app with Swif 3+ 我在Swif 3+的应用程序中使用了这段代码

    var userInfo = notification.userInfo
    if let keyboardFrame = (userInfo?[UIKeyboardFrameBeginUserInfoKey] as? NSValue)?.cgRectValue{
        print(keyboardFrame.height)
        if keyboardFrame.size.height <= 0 { // To fix bug on iOS 11
            if let newKeyboardFrame = (userInfo?[UIKeyboardFrameEndUserInfoKey] as? NSValue)?.cgRectValue{
                print(newKeyboardFrame.height)
            }
        }
    }
    view.layoutIfNeeded()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM