Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Keyboard size changed event in swift?

I am aware of the keyboardWillShow and the keyboardWillHide events by:

override public func viewWillAppear(animated: Bool) {
        NSNotificationCenter.defaultCenter().addObserver(self, selector: "keyboardWillShow:", name: UIKeyboardWillShowNotification, object: nil)
        NSNotificationCenter.defaultCenter().addObserver(self, selector: "keyboardWillHide:", name: UIKeyboardWillHideNotification, object: nil)
    }

But with the new keyboards in iOS8 the keyboard is able to change without dismissing the keyboard and I was wondering how to call a function on keyboard size change. Anyone know? Thanks.

Edit: It is now calling on frame change but using this code:

    if let keyboardSize = (notification.userInfo?[UIKeyboardFrameBeginUserInfoKey] as? NSValue)?.CGRectValue() {
            NSLog("\(keyboardSize.height)")
        }

It returns the old keyboard height for example when the frame changes to "224.0" it returns "253.0" as if the height has not updated by time the code is called, and when it goes have to "253.0" it returns the old height again which is "224.0"

Edit 2: Instead of using "UIKeyboardFrameBeginUserInfoKey", I used "UIKeyboardFrameEndUserInfoKey" and it is now working.

like image 766
uhfocuz Avatar asked Nov 19 '14 03:11

uhfocuz


1 Answers

You want UIKeyboardWillChangeFrameNotification and/or UIKeyboardDidChangeFrameNotification.

See the documentation of UIWindow for all of the keyboard related notifications.

like image 64
A. R. Younce Avatar answered Oct 21 '22 19:10

A. R. Younce