In the app I am building I have a Textfield with a button at the bottom of my screen, and I want to animate them up when the keyboard appears.
For that I use this:
override func viewDidLoad() {
super.viewDidLoad()
NotificationCenter.default.addObserver(self, selector: #selector(self.keyboardWillShow), name: UIResponder.keyboardWillShowNotification, object: nil)
NotificationCenter.default.addObserver(self, selector: #selector(self.keyboardWillHide), name: UIResponder.keyboardWillHideNotification, object: nil)
}
@objc func keyboardWillShow(notification: Notification) {
let keyboardSize = (notification.userInfo? [UIResponder.keyboardFrameEndUserInfoKey] as? NSValue)?.cgRectValue
let keyboardHeight = keyboardSize?.height
if #available(iOS 12.0, *) {
self.submitConstraint.constant = (keyboardHeight! - view.safeAreaInsets.bottom) + 10
}
else {
self.submitConstraint.constant = view.safeAreaInsets.bottom + 10
}
UIView.animate(withDuration: 0.5){
self.view.layoutIfNeeded()
}
}
@objc func keyboardWillHide(notification: Notification){
self.submitConstraint.constant = 34 // or change according to your logic
UIView.animate(withDuration: 0.5){
self.view.layoutIfNeeded()
}
}
but, then a part of the button gets hidden under the suggestions toolbar.
I have googled this, and I found this answer: How to get the height of the keyboard including suggestions bar in swift. I tried the answers, and it didn't work for me, a part of the button still is hidden under the suggestions bar.
Could anybody help me?
Thanks!