iOS键盘不显示在iOS 15.4中

发布于 2025-01-21 21:53:04 字数 1935 浏览 0 评论 0原文

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

这是在显示/解散键盘时向上/向下移动整个视图框架的代码(除了注册观察者和其他内容外)。目前,该应用程序已在大约两年中使用。

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带有上述代码

I'm new to iOS. I've just taken over for another developer. I recently realised that this is a major issue because users cannot use our app. The keyboard gets dismissed as soon as it's shown. So no one can enter anything in the text field/s. It's happening only on iOS 15.4 and 15.4.1

This is the code that moves the entire view frame up/down when the keyboard is shown/dismissed (Apart from registering the observers and stuff). This is currently used in the app for around 2 years now.

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

Is there any other way to handle this for iOS 15.4? The above code is mostly what I find online but it doesn't seem to be an option.

iOS 15.4 with the above code ????????

iOS 15.4 with the above code

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

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

iOS 15.4 with the above code commented out ????????
(Basically not moving the view/s up/down when detecting keyboard show/hide)

iOS 15.4 with the above code commented out

iOS 15.2 (and all lower versions) with the above code working perfectly as intended ????????

iOS 15.2 with the above code working perfectly as intended

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

墨小沫ゞ 2025-01-28 21:53:04

尝试添加诸如this

contentView.bottomanchor.constraint(均等:field.keyboardlayoutguide.topanchor)

之类的约束或检查此链接

也视频教程在这里

Try to add constraints like this

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

or check this link

also video tutorial here

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文