0

Is there a way to get the keyboard size programmatically before the keyboard is presented? In Objective-C

I need to set view.height constraint to be the same as keyboard.height programmatically. And it needs to happen before the keyboard is presented, so the view don't get this ugly constrain animation after the ViewController is presented.

enter image description here

Peter
  • 1,848
  • 4
  • 26
  • 44
  • Possible duplicate of [What is the height of iPhone's onscreen keyboard?](https://stackoverflow.com/questions/11284321/what-is-the-height-of-iphones-onscreen-keyboard) – Nate Birkholz Jan 11 '19 at 22:00
  • @NateBirkholz - And it needs to happen before the keyboard is presented, so the view don't get this ugly constrain animation after the ViewController is presented. – Peter Jan 11 '19 at 22:11
  • 3
    You can use UIKeyboardWillShowNotification instead of UIKeyboardDidShowNotification and get the same sort of information from the call. – Nate Birkholz Jan 11 '19 at 22:31
  • @NateBirkholz - using UIKeyboardWillShowNotification still gives me the animation AFTER the view has been presented, not what I wanted – Peter Jan 11 '19 at 22:37

1 Answers1

1

I assume you present the keyboard by calling becomeFirstResponder on some UI component.

If the keyboard appears after your view is presented, you should check where that call is performed. Calling it in viewDidLoad or similarly early should cause the keyboard to be shown as the view animates in.


Your layout should also handle the keyboard changes properly. The keyboard size can change even after it's presented. For example the emoji/quick type keyboards are taller than the default keyboard.

You should perform your constraint changes in a combination of UIKeyboard[Will/Did]ShowNotification, UIKeyboard[Will/Did]HideNotification and UIKeyboardWillChangeFrameNotification. In your case, UIKeyboardWillShowNotification should do the trick.

The userInfo dictionary contains a lot of information about the keyboard. You find the final frame of the keyboard in UIKeyboardFrameEndUserInfoKey. If you animate the changes in your layout, you can use values in UIKeyboardAnimationCurveUserInfoKey and UIKeyboardAnimationDurationUserInfoKey to animate with the same animation as the keyboard.

- (void)viewDidLoad {
    [super viewDidLoad];

    // Don't forget to remove the observer when appropriate.
    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(keyboardWillShow:)
                                                 name:UIKeyboardWillShowNotification
                                               object:nil];
    [self.textField becomeFirstResponder];
}

- (void)keyboardWillShow:(NSNotification *)notification {
    CGFloat keyboardHeight = [notification.userInfo[UIKeyboardFrameEndUserInfoKey] CGRectValue].size.height;

    [self.viewHeightConstraint setConstant:keyboardHeight];

    // You can also animate the constraint change.
}

Such setup will also work if the keyboard is presented from the get-go.

teddy
  • 217
  • 1
  • 6