iOS 键盘在 iOS 15.4 中不显示

标签 ios swift xcode ios-15.4 ios-15.4.1

我是 iOS 新手。我刚刚接手了另一个开发人员。我最近意识到这是一个主要问题,因为用户无法使用我们的应用程序。键盘一显示就消失了。所以没有人可以在文本字段中输入任何内容。它仅发生在 iOS 15.4 和 15.4.1 上

这是在显示/关闭键盘时向上/向下移动整个 View 框架的代码(除了注册观察者和其他东西)。这目前在应用程序中使用了大约 2 年。

override func viewWillAppear(_ animated: Bool) {
  registerForKeyboardNotifications()
}

func registerForKeyboardNotifications() {
  // UIResponser.keyboardWillShowNotification is supplemented 
  // with UIResponser.keyboardDidShowNotification only in the second gif image below
  NotificationCenter.default.addObserver(self, selector: #selector(keyboardWillShow(_:)), name: UIResponder.keyboardWillShowNotification, object: nil)
        
  NotificationCenter.default.addObserver(self, selector: #selector(keyboardWillHide(_:)), name: UIResponder.keyboardWillHideNotification, object: nil)
        
}


@objc func keyboardWillShow(_ sender: Foundation.Notification) {
        
  let info = sender.userInfo!
        
  let keyboardFrame: CGRect = (info[UIResponder.keyboardFrameEndUserInfoKey] as! NSValue).cgRectValue
        
    UIView.animate(withDuration: 0.1, animations: { () -> Void in    
      if self.view.frame.origin.y == self.currentOrigin {
        self.view.frame.origin.y -= keyboardFrame.size.height
      }
    })
        
}
    
@objc func keyboardWillHide(_ sender: Foundation.Notification) {
        
  UIView.animate(withDuration: 0.1, animations: { () -> Void in
      self.view.frame.origin.y = self.currentOrigin
  })
        
}

override func viewWillDisappear(_ animated: Bool) {
  deregisterFromKeyboardNotifications()
}

func deregisterFromKeyboardNotifications(){
        
  NotificationCenter.default.removeObserver(self, name: UIResponder.keyboardWillShowNotification, object: nil)
        
  NotificationCenter.default.removeObserver(self, name: UIResponder.keyboardWillHideNotification, object: nil)
        
}

对于 iOS 15.4,还有其他方法可以处理这个问题吗?上面的代码大部分是我在网上找到的,但它似乎不是一个选项。

iOS 15.4 以上代码👇🏻

iOS 15.4 with the above code

iOS 15.4 具有上述代码,但上面的 keyboardWillShow 函数由 keyboardDidShow 观察者触发 👇🏻

iOS 15.4 with the above code but the keyboardWillShow function above being triggered by the keyboardDidShow observer instead

iOS 15.4 上面的代码被注释掉了👇🏻 (检测键盘显示/隐藏时基本上不上下移动 View )

iOS 15.4 with the above code commented out

iOS 15.2(和所有更低版本)上面的代码可以完美地按预期工作👇🏻

iOS 15.2 with the above code working perfectly as intended

最佳答案

尝试像这样添加约束

contentView.bottomAnchor.constraint(equalTo: view.keyboardLayoutGuide.topAnchor)

或检查此 link

还有视频教程here

关于iOS 键盘在 iOS 15.4 中不显示,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/71904326/

相关文章:

ios - 如何使用 addObserver 闭包方法在 Swift 5 中删除Observer

ios - 以编程方式将 View 添加到 tableView 单元格中,导致滚动不稳定且缓慢

ios - NSURL 图像加载减慢应用程序打开时间 - iOS

ios - NSURLConnection willSendRequestForAuthenticationChallenge 安全吗

ios - 使用空格进行 GET 查询

ios - 如何在 Swift 中使用反射设置成员变量值?

ios - 如何正确地从 xcode 框架调用 loadView()

ios - 无法将类型 'Swift.UInt64' (0x10dddde88) 的值转换为 'Swift.AnyObject' (0x12129d018)

ios - 与 ios swift 本地数据库和服务器同步数据

node.js - 在 Apple M1 (Big Sur) 上使用 node-gyp 安装 native Node.js 附加组件时出错