16

I have found the iPhone's keyboard bounds in the apple documentation, but I can't find the iPad's keyboard bounds. Could you please help me?

Infinite Possibilities
  • 7,415
  • 13
  • 55
  • 118

4 Answers4

60

The entire answer in code looks like this. First you need to register for the notifications:

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardDidShow:) name:UIKeyboardDidShowNotification object:nil];

and there are more here. Note that you'll need to get rid of them, too (use removeObserver).

Then you need a method that gets the notification to get the size. Note that the size is, at first, not rotated (since the UIWindow doesn't rotate. Its contents do).

- (void) keyboardDidShow:(NSNotification*)notification {
        CGRect keyboardFrame = [[[notification userInfo] objectForKey:UIKeyboardFrameEndUserInfoKey] CGRectValue];
        NSLog(@"keyboard frame raw %@", NSStringFromCGRect(keyboardFrame));

        UIWindow *window = [[[UIApplication sharedApplication] windows]objectAtIndex:0];
        UIView *mainSubviewOfWindow = window.rootViewController.view;
        CGRect keyboardFrameConverted = [mainSubviewOfWindow convertRect:keyboardFrame fromView:window];
        NSLog(@"keyboard frame converted %@", NSStringFromCGRect(keyboardFrameConverted));
}

Obviously, if you have a reference to your mainSubviewOfWindow by some other means, use it.

Dan Rosenstark
  • 68,471
  • 58
  • 283
  • 421
18

For iPhone in portrait 216 pixels, landscape 162 pixels, for iPad in portrait it's 264 pixels and in landscape 352 pixels. This is valid for US keyboard in 2010.

These sizes can be different for other languages, and might change for US as well.

Peter Robert
  • 1,292
  • 11
  • 18
  • use 116 for iPhone in landscape. 162 was still to large. – tallen11 Feb 13 '11 at 16:22
  • 9
    You shouldn't rely on these measurements. Different languages use different keyboard sizes. Even the size of the US English keyboard may change in future versions of iOS. You should always query the size of the keyboard programmatically: see @Infinity's answer for how. – titaniumdecoy Aug 03 '11 at 00:50
  • 6
    Wrong. Keyboard with different languages can have different size. e.g. Japanese keyboard is different than english. Use Yar's method to see the size of the current keyboard. – Enrico Susatyo Dec 18 '11 at 10:20
11

Please note that if user choose to use "split" keyboard on iPad, then UIKeyboardDidShowNotification/*UIKeyboardDidHideNotification* notifications will not be fired. Instead, UIKeyboardDidChangeFrameNotification notification will be fired on both show and hide. You will have to analyze keyboardFrame.origin.y to figure out what exactly happen (show or hide).

Andrei Tchijov
  • 559
  • 5
  • 11
8

I just found it if somebody else needs it.
Keyboard Notification User Info Keys

Infinite Possibilities
  • 7,415
  • 13
  • 55
  • 118