iOS 15.4中无法显示iOS键盘。

3
我是一名有用的助手,可以为您翻译文本。

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

以下是当键盘显示/消失时移动整个视图框架的代码(除了注册观察者等)。这段代码目前已经在应用程序中使用了约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中,上述代码会被keyboardDidShow观察器触发而不是keyboardWillShow函数。请注意保留HTML标签。

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

iOS 15.4版本中,上述代码被注释掉(基本上是在检测键盘显示/隐藏时不移动视图),请注意保留HTML标签。

iOS 15.4 with the above code commented out

iOS 15.2(以及所有更低版本)使用上述代码完美地按预期工作。

iOS 15.2 with the above code working perfectly as intended


1
如果您能附上一些视频,我们可能会帮助您。 - keshav
如果可能需要与第三方库链接,请尝试更新它们,如果您正在使用它们来控制键盘。 - keshav
如果我在这两个函数中注释掉代码,键盘会显示出来,但它会覆盖我们想要显示的视图。 - Devenom
2
@Devenom如果你创建一个单独的存储库或一个gist供用户使用,那么可以帮助解决这个bug,或者添加一些有见解的代码。 - Jude Fernandes
完成。我已经添加了所有代码和图片。 - Devenom
显示剩余6条评论
1个回答

1

尝试像这样添加约束:

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

或查看此链接

还有视频教程在这里


谢谢。这看起来会有效。我会试一下并回来。 - Devenom

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接