I am trying to push my view up when my keyboard is called. I have been able to do have this work on the native iOS keyboard but there seems to be an error with custom keyboards. The first time that the keyboard is launched the view moves up 55pixels less than it should. Every time the keyboard is launched after that first time works properly.
I have been able to find a similar question but the answers don't seem to work for me: can't get correct value of keyboard height in iOS8
Here is my current code for pushing the view up:
override func viewDidLoad() {
super.viewDidLoad()
NotificationCenter.default.addObserver(self, selector: #selector(keyboardWillShow), name: NSNotification.Name.UIKeyboardWillShow, object: nil)
NotificationCenter.default.addObserver(self, selector: #selector(keyboardWillHide), name: NSNotification.Name.UIKeyboardWillHide, object: nil)
}
func keyboardWillShow(notification: NSNotification) {
if let keyboardSize = (notification.userInfo?[UIKeyboardFrameBeginUserInfoKey] as? NSValue)?.cgRectValue {
if self.view.frame.origin.y == self.partialView{
self.view.frame.origin.y -= keyboardSize.height
print(self.view.frame.origin.y)
}
}
}
func keyboardWillHide(notification: NSNotification) {
if let keyboardSize = (notification.userInfo?[UIKeyboardFrameBeginUserInfoKey] as? NSValue)?.cgRectValue {
if self.view.frame.origin.y != 0{
self.view.frame.origin.y += keyboardSize.height
print(self.view.frame.origin.y)
}
}
}
Hoping you can help me fix the issue with the first call of the keyboard. I have tried to convert the keyboard frame from window reference, however, this was unsuccessful as well.