0

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.

ajayb
  • 633
  • 1
  • 9
  • 23
  • Only [one of the answers](https://stackoverflow.com/a/27144726/1226963) in the question you linked do one important step - convert the frame from window coordinates to local view coordinates. That is also shown in the question and answer I linked. – rmaddy Jul 17 '17 at 04:02
  • @rmaddy TS push the whole view up, not subview, so dont need convert frame, your duplicate question doesnt seems to have anything to do with the wrong height from custom keyboard? – Tj3n Jul 17 '17 at 04:06
  • @Tj3n What is "TS"? And the important missing piece of info is the need to convert the keyboard frame from window to local coordinates. – rmaddy Jul 17 '17 at 04:07
  • @rmaddy thread starter...what do you mean he didn't, `notification.userInfo?[UIKeyboardFrameBeginUserInfoKey]` this piece of code is to get the height of the keyboard, should be only need the height, not really need to convert it to rect for anything – Tj3n Jul 17 '17 at 04:09
  • @Tj3n See the answer I linked in my first comment and the answer to the duplicate question. See how both call `convert` to convert the keyboard frame from window reference frame to local view reference frame? That step is required to work properly. – rmaddy Jul 17 '17 at 04:12
  • @Tj3n It's even documented by Apple. See the documentation for `UIKeyboardFrameBeginUserInfoKey` and related keys. – rmaddy Jul 17 '17 at 04:13
  • @rmaddy The 2nd answer dont use `convert`, he only use the height, pls check again, and even if you are right, it still have nothing to do with the custom keyboard height, that happen because `keyboardWillShow ` get called multiple times if the height is not Apple keyboard – Tj3n Jul 17 '17 at 04:19

0 Answers0