【问题标题】:Keyboard notification not called with small keyboard小键盘未调用键盘通知
【发布时间】:2021-07-10 07:07:52
【问题描述】:

在我的代码中,当键盘很大时,通常会调用键盘打开/关闭的通知。但是一旦它变小,在手指之间挤压键盘,这些通知就不再被调用了。有人遇到类似问题吗?

这就是我观察通知的方式:

let notificationCenter = NotificationCenter.default
notificationCenter.addObserver(self, selector: #selector(self.keyboardWillBeShown(_:)), name: NSNotification.Name.UIKeyboardWillShow, object: nil)
notificationCenter.addObserver(self, selector: #selector(self.keyboardDidShown(_:)), name: NSNotification.Name.UIKeyboardDidShow, object: nil)
notificationCenter.addObserver(self, selector: #selector(self.keyboardWillBeHidden(_:)), name: NSNotification.Name.UIKeyboardWillHide, object: nil)

【问题讨论】:

    标签: ios swift ipad notifications keyboard


    【解决方案1】:

    这是预期的行为。默认情况下,键盘会缩小屏幕尺寸,因此您必须挤压项目以使其适合。

    但是当您“用手指捏住键盘”时,它会开始漂浮在您的视线上方,因此不再需要捏住。

    您可以使用UIResponder.keyboardWillChangeFrameNotification 获取键盘外观信息,如下所示:

    func keyboardWillChangeFrame(_ notification: Notification) {
        guard
            let userInfo = notification.userInfo,
            let keyboardFrameEndRect = userInfo[UIResponder.keyboardFrameEndUserInfoKey] as? CGRect,
            let animationCurveOption = userInfo[UIResponder.keyboardAnimationCurveUserInfoKey] as? Int
        else { return }
        print(keyboardFrameEndRect, animationCurveOption)
        if keyboardFrameEndRect.isEmpty || // Floating keyboard is HIDING or BEING DRAGGED by the user
            keyboardFrameEndRect.origin.y >= UIScreen.main.bounds.height // Split keyboard is moving off screen
        {
            // When animation curve is zero, the keyboard is being hidden. (Otherwise, it's being moved)
            if animationCurveOption != 0 {
                print("KEYBOARD IS HIDING")
            } else {
                // e.g. ignore
                print("FLOATING KEYBOARD IS MOVING")
            }
        } else {
            print("KEYBOARD IS SHOWING")
        }
    }
    

    附言UIResponder.keyboardWillChangeFrameNotification 遵循当前的 swift 语法,而不是 NSNotification.Name.UIKeyboardWillChangeFrame

    【讨论】:

      【解决方案2】:

      您还应该注册UIResponder.keyboardFrameEndUserInfoKey(用于在动画结束时检索键盘帧的用户信息键。)以观察键盘帧的变化。

      【讨论】:

      • 您的答案可以通过额外的支持信息得到改进。请edit 添加更多详细信息,例如引用或文档,以便其他人可以确认您的答案是正确的。你可以找到更多关于如何写好答案的信息in the help center
      【解决方案3】:
      notificationCenter.addObserver(self, selector: #selector(adjustForKeyboard), name: UIResponder.keyboardWillChangeFrameNotification, object: nil)
      
      @objc func adjustForKeyboard(notification: Notification) {
          guard let keyboardValue = notification.userInfo?[UIResponder.keyboardFrameEndUserInfoKey] as? NSValue else { return }
      
          let keyboardScreenEndFrame = keyboardValue.cgRectValue
          let keyboardViewEndFrame = view.convert(keyboardScreenEndFrame, from: view.window)
           
          //You will get notifications event here
          if notification.name == UIResponder.keyboardWillHideNotification {
          
          } else {
         
          }
      }
      

      adjustForKeyboard() 方法有很多工作要做。首先,它将接收一个类型为 Notification 的参数。这将包括通知的名称以及包含通知特定信息的字典,称为 userInfo。

      使用键盘时,字典将包含一个名为 UIResponder.keyboardFrameEndUserInfoKey 的键,告诉我们键盘在完成动画后的帧。这将是 NSValue 类型,而后者又是 CGRect 类型。 CGRect 结构体同时包含一个 CGPoint 和一个 CGSize,因此它可以用来描述一个矩形。

      【讨论】:

        猜你喜欢
        • 2018-12-14
        • 1970-01-01
        • 2019-02-26
        • 2018-02-27
        • 2020-02-17
        • 2022-12-17
        • 2012-11-30
        • 2014-01-28
        • 2018-01-23
        相关资源
        最近更新 更多