I have a UIView that isn't moving properly when the keyboard appears. There is a UITextView in a UIView that I use to enter text. If I select the TextView to enter text, the keyboard appears but the UIView doesn't move THE FIRST TIME. If I tap the background and make the keyboard disappear and then tap on the TextView again, then the UIView moves up properly. Does anyone know what's happening here?
class ChatViewController: UIViewController, CNContactPickerDelegate, UISearchBarDelegate, UITableViewDelegate, UITableViewDataSource, UIToolbarDelegate, UITextFieldDelegate, UITextViewDelegate {
@IBOutlet weak var composeTextView: UITextView!
@IBOutlet weak var composeViewBottomConstraint: NSLayoutConstraint!
override func viewDidLoad() {
super.viewDidLoad()
composeTextView.delegate = self
}
func textViewDidBeginEditing(_ textView: UITextView) {
UIView.animate(withDuration: 0.5){
NotificationCenter.default.addObserver(self, selector: #selector(self.keyboardWillShow), name: .UIKeyboardWillShow, object: nil)
}
self.view.layoutIfNeeded()
}
@objc func keyboardWillShow(notification: Notification) {
let keyboardSize = (notification.userInfo?[UIKeyboardFrameEndUserInfoKey] as? NSValue)?.cgRectValue
let keyboardHeight = keyboardSize?.height
if #available(iOS 11.0, *){
self.composeViewBottomConstraint.constant = keyboardHeight! - view.safeAreaInsets.bottom
}
else {
self.composeViewBottomConstraint.constant = view.safeAreaInsets.bottom
}
self.view.layoutIfNeeded()
}
}