0

I want to implement that function into my textfield's function. I tried it different ways but I couldn't succeed.
Here is the code:

NotificationCenter.default.addObserver(self, selector: #selector(keyboardWillShow), name: UIResponder.keyboardWillShowNotification, object: nil)
@objc func keyboardWillShow(notification: NSNotification) {
    if let keyboardSize = (notification.userInfo?[UIResponder.keyboardFrameBeginUserInfoKey] as? NSValue)?.cgRectValue {
        if self.view.frame.origin.y == 0 {
            self.view.frame.origin.y -= keyboardSize.height
        }
    }
}
Vadim Kotov
  • 8,084
  • 8
  • 48
  • 62
caneraltuner
  • 263
  • 1
  • 3
  • 12
  • Possible duplicate of [NSNotificationCenter Swift 3.0 on keyboard show and hide](https://stackoverflow.com/questions/41718520/nsnotificationcenter-swift-3-0-on-keyboard-show-and-hide) – Rakesha Shastri Dec 29 '18 at 07:56

1 Answers1

1

You're using key UIResponder.keyboardFrameBeginUserInfoKey which cause that you get wrong height.

Use this key instead:

UIResponder.keyboardFrameEndUserInfoKey
Robert Dresler
  • 10,580
  • 2
  • 22
  • 40