【问题标题】:iOS 11 - Keyboard Height is returning 0 in keyboard notificationiOS 11 - 键盘高度在键盘通知中返回 0
【发布时间】:2018-01-23 04:28:14
【问题描述】:

我一直在使用键盘通知没有任何问题,并获得了键盘的准确高度。

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

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

但在 iOS 11 中,调用通知时键盘的大小为 0。

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

【问题讨论】:

    标签: ios objective-c nsnotificationcenter


    【解决方案1】:

    使用这个:

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

    对于 Swift,您可以使用:

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

    【讨论】:

    • 你拯救了我的一天
    • 谢谢,没想到
    • 从 UIKeyboardFrameBeginUserInfoKey 更改为 UIKeyboardFrameEndUserInfoKey 成功了,谢谢。
    • 谢谢,投资了半天,还是搞不明白。感谢您节省了我的另一半天。
    • @Akshay Just startframe 和 endframe 在各自的屏幕范围内的键盘。
    【解决方案2】:

    替换UIKeyboardFrameBeginUserInfoKey

    UIKeyboardFrameEndUserInfoKey

    以下内容来自 Apple Docs。

    UIKeyboardFrameBeginUserInfoKey- NSValue 对象的键 包含标识键盘起始帧的 CGRect 在屏幕坐标中。

    UIKeyboardFrameEndUserInfoKey - NSValue 对象的键 包含一个标识键盘结束帧的 CGRect 屏幕坐标。

    【讨论】:

    • Apple 需要更新他们的示例代码,因为它不再适用于 iOS 11。同样来自 Apple 文档,他们使用 UIKeyboardFrameBeginUserInfoKey:developer.apple.com/library/content/documentation/…
    • 解决了我的问题!感谢您的建议!
    • 有人可以像我 5 岁一样向我解释这两者之间的区别是什么,以及为什么在让 UIScrollView 自动滚动时会有所不同?我真的很感激。谢谢!
    • 使用 numPad 点击文本字段后无法正常工作。如果在 numPad 之后我点击到常规文本字段,则键盘不再向上并且当它关闭视图时会关闭
    【解决方案3】:

    试试这个:

    UIKeyboardFrameBeginUserInfoKey 替换为UIKeyboardFrameEndUserInfoKey

    【讨论】:

    • [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillChangeFrame:) name:UIKeyboardWillChangeFrameNotification object:nil];方法keyboardWillChangeFrame:不只是运行一次,可能两次,甚至更多。也许某些键盘有时顶部有特殊的工具栏,这个方法会运行多次。所以,你应将 UIKeyboardFrameBeginUserInfoKey 替换为 UIKeyboardFrameEndUserInfoKey 以获取键盘的最终帧。
    【解决方案4】:

    这个问题发生在 iOS 11 上。

    替换

    “UIKeyboardFrameBeginUserInfoKey”与“UIKeyboardFrameEndUserInfoKey”

    如下图可以解决问题

    Objective-C 代码:

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

    斯威夫特 2.3:

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

    斯威夫特 3:

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

    【讨论】:

      【解决方案5】:

      我在使用 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,它为我解决了这个问题。

      【讨论】:

      • 我注意到了同样的事情。它似乎也只适用于键盘第一次显示。其他视图控制器上的键盘第一次显示该视图控制器时将高度报告为 0。
      【解决方案6】:

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

      它适用于具有安全区域和非安全区域设备的设备。

      代码:

      @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
          }
      }
      

      【讨论】:

        【解决方案7】:

        您的方法是在显示之前尝试获取帧高度,这就是为什么它应该是 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
        }
        

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

        【讨论】:

          【解决方案8】:

          尽管我们从 SafeArea 调整 BottomSpace Constraints 时获得了准确的键盘高度,但它会产生更多的空白空间。因此,如果我们要调整 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()
                        }
          

          【讨论】:

            猜你喜欢
            • 2015-01-04
            • 2011-10-29
            • 2015-12-04
            • 2019-04-09
            • 2015-03-05
            • 2015-11-14
            • 1970-01-01
            • 2019-05-23
            • 2014-11-19
            相关资源
            最近更新 更多