I\'m looking for two numbers here: the height in portrait and the height in landscape. Don\'t answer in cm or inches, but rather in pixels.
I've voted down a couple of answers on this page, as they are dangerously misleading.
Certainly, with iOS 8, you cannot use a hard-coded keyboard height.
Here's my app, running on an iPhone, to give my reason why.
Grab your ruler, and tell me, what is the height of the onscreen keyboard..?
Ahh... you can't do it, can you ?
The height can vary, if the user turns on the auto-type bar at the top of the onscreen keyboard (which, by the way, triggers off a second "keyboardWillShow
" event).
The same is true for the onscreen keyboard on the iPad.
Bottom line: sorry folks, you must measure the keyboard height in a keyboardWillShow
event.
You will hit problems otherwise.
- (void)keyboardWillShow:(NSNotification *)notification {
NSDictionary *info = [notification userInfo];
NSValue *kbFrame = [info objectForKey:UIKeyboardFrameEndUserInfoKey];
CGRect keyboardFrame = [kbFrame CGRectValue];
keyboardFrame = [self.view convertRect:keyboardFrame fromView:nil];
CGFloat heightOfKeyboard = keyboardFrame.size.height;
NSLog(@"The keyboard is now %d pixels high.", (int)heightOfKeyboard);
}
Btw, don't forget that call to convertRect
in the code above.
Without it, I found that, on an iPad running in landscape with iOS 8, sometimes the keyboard would be reported as having a height of 1024 pixels..
Just one more thing...
Today, I was working on a custom control I'd written, which appears at the bottom of the iPhone/iPad screen, and realised I hadn't taken the onscreen keyboard height into consideration.
I didn't want to add keyboardWillShow/Hide notifications, as this little control was generic, able to be called from any of my screens.
My solution to find the height of the onscreen keyboard, if it was currently visible, was to use the excellent visibleKeyboardHeight function from the free SVProgressHUD
control.
That control also needed the height of the onscreen keyboard, so its notification message UIView
s would appear vertically-centered.
It worked a treat... so if you do need a quick'n'dirty method of grabbing the keyboard height, grab a copy of this code, and look in the SVProgressHUD.m file.
Download SVProgressHUD from GitHub
The answer is: it depends.
I am aware of 16 different heights the keyboard can be, and it can be in any position.
It is different for:
I have attached an image showing some different possible configurations.
Rather than enumerating all 16 possibilities, I think you want to observe UIKeyboardWillShowNotification
/UIKeyboardWillHideNotification
and UIKeyboardDidChangeFrameNotification
(iOS 5.0+), and then get the keyboard's frame from those events.
You can get the keyboard height from the userInfo dictionary returned by the will/did show/hide keyboard notification, as provided by the UIWindow class (reference guide link). This is a much better way of getting the height than hard-coding because you get the following benefits:
In fact, the answers to this question that give an exact number most likely got this number by looking at the results returned within this dictionary!
In iOS 7, for iPad it is 402 for Landscape and 314 for Portrait
There was an issue in iOS 7 where the height and width were not swapping in landscape mode, but the issue was fixed in iOS 8.
So, here is the piece of code that would always return the height for iOS7 or iOS8, thereby eliminating the version specific check in your code.
- (void)keyboardWasShown:(NSNotification*)aNotification
{
NSDictionary* info = [aNotification userInfo];
CGSize kbSize = [[info objectForKey:UIKeyboardFrameBeginUserInfoKey] CGRectValue].size;
//Always gives the height as the height is of smaller dimension
CGFloat height = (kbSize.height>kbSize.width)?kbSize.width:kbSize.height;
}
You can find this out programmatically (through the simulator if you dont have a device).
http://developer.apple.com/library/ios/#documentation/StringsTextFonts/Conceptual/TextAndWebiPhoneOS/KeyboardManagement/KeyboardManagement.html#//apple_ref/doc/uid/TP40009542-CH5-SW1