我是 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 具有上述代码,但上面的 keyboardWillShow
函数由 keyboardDidShow
观察者触发 👇🏻
iOS 15.4 上面的代码被注释掉了👇🏻 (检测键盘显示/隐藏时基本上不上下移动 View )
iOS 15.2(和所有更低版本)上面的代码可以完美地按预期工作👇🏻
最佳答案
尝试像这样添加约束
contentView.bottomAnchor.constraint(equalTo: view.keyboardLayoutGuide.topAnchor)
或检查此 link
还有视频教程here
关于iOS 键盘在 iOS 15.4 中不显示,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/71904326/