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 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.2 (and all lower versions) with the above code working perfectly as intended
Try to add constraints like this
contentView.bottomAnchor.constraint(equalTo: view.keyboardLayoutGuide.topAnchor)
or check this link
also video tutorial here