iOS 11 - 键盘高度在键盘通知中返回 0

iOS 11 - Keyboard Height is returning 0 in keyboard notification

我一直在毫无问题地使用键盘通知并获得键盘的准确高度。

- (void)keyboardDidShow:(NSNotification *) notification{
    CGSize keyboardSize = [[[notification userInfo] objectForKey:UIKeyboardFrameBeginUserInfoKey] CGRectValue].size;

    NSLog(@"%f",keyboardSize.height);}

但是 iOS 11 调用通知时键盘大小为 0。

这种情况下出现的问题是什么?我正在使用 xcode 9 Beta 5

使用这个:

CGSize keyboardSize = [[[notification userInfo] objectForKey:UIKeyboardFrameEndUserInfoKey] CGRectValue].size;

对于Swift,您可以使用:

let keyboardSize = (notification.userInfo![UIKeyboardFrameEndUserInfoKey] as! NSValue).cgRectValue.size

替换UIKeyboardFrameBeginUserInfoKey

UIKeyboardFrameEndUserInfoKey

以下内容来自 Apple 文档。

UIKeyboardFrameBeginUserInfoKey- The key for an NSValue object containing a CGRect that identifies the start frame of the keyboard in screen coordinates.

UIKeyboardFrameEndUserInfoKey - The key for an NSValue object containing a CGRect that identifies the end frame of the keyboard in screen coordinates.

试试这个:

UIKeyboardFrameBeginUserInfoKey替换为UIKeyboardFrameEndUserInfoKey

我在使用 Xcode 版本 9.0 (9A235) 时遇到了类似的问题;尽管我使用的是 Swift。在我的 keyboardWillShow 方法中,我写了以下内容:

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

    let heightValue = keyboardSize.height    

    ...
}

奇怪的是,第一次调用keyboardWillShow时,heightValue是216.0,后来调用就变成0了!也许这是一个 Xcode 错误。

我用 UIKeyboardFrameEndUserInfoKey 替换了 UIKeyboardFrameBeginUserInfoKey,它解决了我的问题。

此问题发生在 iOS 11.

Replace

"UIKeyboardFrameBeginUserInfoKey" with "UIKeyboardFrameEndUserInfoKey"

as shown below would fix the issue

Objective-C代码:

CGSize keyboardSize = [[[notification userInfo] objectForKey:UIKeyboardFrameEndUserInfoKey] CGRectValue].size;

Swift 2.3 :

let keyboardSize = (NfnPsgVar.userInfo![UIKeyboardFrameEndUserInfoKey] as? NSValue)?.CGRectValue().size

Swift 3 :

let keyboardSize = (notification.userInfo![UIKeyboardFrameEndUserInfoKey] as! NSValue).cgRectValue.size

您的方法是尝试在显示之前获取框架高度,这就是为什么它应该为 0,我不确定为什么它不是第一次尝试!下面是如何在 swift 4.2 中正确获取键盘高度的示例:

func keyboardWillShow(notification: Notification) {

    guard let userInfo = notification.userInfo else { return }

    guard var keyboardFrame: CGRect = (userInfo[UIResponder.keyboardFrameEndUserInfoKey] as? NSValue)?.cgRectValue else { return }

    keyboardFrame = view.convert(keyboardFrame, from: nil)

    let keyboardHeight = keyboardFrame.height
}

这将正确提供键盘框架属性键盘出现之前。

使用以下代码计算键盘高度。

It is working for both devices with the safe area and non-safe area devices.

代码:

@objc func keyboardWillShow(notification: Notification) {
    guard let keyboardFrame = notification.userInfo[UIResponder.keyboardFrameEndUserInfoKey] as? NSValue else {
            return
        }    
    let keyboardHeight: CGFloat                    
    if #available(iOS 11.0, *) {
       let window = UIApplication.shared.keyWindow
       let bottomPadding = window?.safeAreaInsets.bottom ?? 0.0
       keyboardHeight = keyboardFrame.cgRectValue.height - bottomPadding
    } else {
       keyboardHeight = keyboardFrame.cgRectValue.height
    }
}

尽管当我们从 SafeArea 调整 BottomSpace Constraints 时我们得到了准确的键盘高度,但它使更多空 space。因此,如果我们要调整 bottomSpace 约束,我们必须从键盘高度中减去 safeArea 边距。

    @objc func keyBoardWillShow(notification: NSNotification) {
                  
    
                  if let keyboardFrame: NSValue = notification.userInfo?[UIResponder.keyboardFrameEndUserInfoKey] as? NSValue {
                      var bottomPadding: CGFloat = 0
                      //IF device is latest including iPhone x, it will have bottom padding as safeArea Insets. So we have to subtract that from REAL Keyboard HEIGHT.
                      if let window = UIApplication.shared.windows.first{
                        bottomPadding = window.safeAreaInsets.bottom
                      }
    
                      let keyboardRectangle = keyboardFrame.cgRectValue
                      let keyboardHeight = keyboardRectangle.height - bottomPadding
                      bottomSpaceConstraint.constant = keyboardHeight
                  }
                  self.view.layoutIfNeeded()
              }