Lachtan Lachtan - 8 months ago 350
Swift Question

How to get height of Keyboard? (swift)

The height of a keyboard on varying iOS devices is different. Does anybody know how I can get height of a device's keyboard programmatically? Thank you.

roc roc

In Swift:

You can get the keyboard height by subscribing to the UIKeyboardWillShowNotification notification. (Assuming you want to know what the height will be before it's shown).

Something like this:

    NSNotificationCenter.defaultCenter().addObserver(self, selector: "keyboardWillShow:", name: UIKeyboardWillShowNotification, object: nil)

Then you can access the height in some function like this:

func keyboardWillShow(notification:NSNotification) {
    let userInfo:NSDictionary = notification.userInfo!
    let keyboardFrame:NSValue = userInfo.valueForKey(UIKeyboardFrameEndUserInfoKey) as! NSValue
    let keyboardRectangle = keyboardFrame.CGRectValue()
    let keyboardHeight = keyboardRectangle.height