I have bottomView (the one with black border in the images) its bottom anchor has a constraint to bottom view safeAreaLayoutGuide, but when keyboard will show the bottomView became toolbar to the keyboard.
Here every thing was working as expected in iOS 14 (like image 1 and 3) but when running the project in iOS 13 I found that safeAreaInsets changes its value after the keyboard dismissed (red space increases its height like image 2)
I've tried some of the solutions that mentioned here but it didn't help can anyone suggest a solution for this?
@objc func adjustForKeyboard(notification: Notification) {
guard let keyboardValue = notification.userInfo?[UIResponder.keyboardFrameEndUserInfoKey] as? NSValue else { return }
let keyboardScreenEndFrame = keyboardValue.cgRectValue
let keyboardViewEndFrame = view.convert(keyboardScreenEndFrame, from: view.window)
if notification.name == UIResponder.keyboardWillHideNotification {
bottomViewConstraint?.constant = 0
NotesText.contentInset = .zero
} else {
bottomViewConstraint?.constant = -(keyboardViewEndFrame.height - self.view.safeAreaInsets.bottom)
NotesText.contentInset = UIEdgeInsets(top: 0, left: 0, bottom: -(keyboardViewEndFrame.height + bottomViewConstraint!.constant), right: 0)
//FIXME:- force unwrap
}
NotesText.scrollIndicatorInsets = NotesText.contentInset
let selectedRange = NotesText.selectedRange
NotesText.scrollRangeToVisible(selectedRange)
}
and called this function in viewDidAppear also tried to called it at viewSafeAreaInsetsDidChange but the behavior didn't change
override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)
addKeyboardNotification()
// Show keyboard by default
NotesText.becomeFirstResponder()
}
Keyboard notification function:
fileprivate func addKeyboardNotification() {
let notificationCenter = NotificationCenter.default
notificationCenter.addObserver(self, selector: #selector(adjustForKeyboard), name: UIResponder.keyboardWillHideNotification, object: nil)
notificationCenter.addObserver(self, selector: #selector(adjustForKeyboard), name: UIResponder.keyboardWillChangeFrameNotification, object: nil)
}