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.
The portrait height is 264 while the landscape height is 352.
I know this is a late answer, but I just came across this question myself.
- (void) keyboardWasShown:(NSNotification *)nsNotification {
NSDictionary *userInfo = [nsNotification userInfo];
CGSize kbSize = [[userInfo objectForKey:UIKeyboardFrameBeginUserInfoKey] CGRectValue].size;
NSLog(@"Height: %f Width: %f", kbSize.height, kbSize.width);
// Portrait: Height: 264.000000 Width: 768.000000
// Landscape: Height: 352.000000 Width: 1024.000000
}
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWasShown:) name:UIKeyboardDidShowNotification object:nil];
However my problem is, this app is landscape and this code doesn't seem to take that into effect. It says my Height is 1024 and my width is 396. –
Eisenstein 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:
- landscape/portrait,
- webview/non-webview,
- asian character selection style, regular language
- split keyboard / non-split
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.
On the iPad, the keyboard size can also depend on the language (e.g. the japanese keyboard is taller). Another reason to query it programmatically.
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:
- Device independence (iPhone/iPod or iPad)
- Resolution independence (iPhone 3G screen/retina display/iPad display/future displays).
- Keyboard type/style independence (different on-screen keyboards may have difference sizes).
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!
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.
You can find this out programmatically (through the simulator if you dont have a device).
Heights are: PORTRAIT = 264 LANDSCAPE = 352
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;
}
I don't know what it is, but you could figure it out easily enough.
Surf on your iPad to a page with this...
JavaScript
var totalHeight = window.innerHeight;
document.getElementById('what-is-my-height').onclick = function() {
alert(totalHeight - window.innerHeight);
};
HTML
<button id="what-is-my-height">Bring up keyboard, then push me</button>
Click the button (after you have brought up the on screen keyboard), and it should tell you the height. Repeat for the other orientation.
I think there are more troubles than are solved in others answers. For example, you can end up with this keyboard:
This happens when you click "hide" button on iPad keyboard on iOS 9. This keyboard has still full
size in notification info from Mike Gledhill
answer. (in UIKeyboardFrameEndUserInfoKey
height of keyboard is higher than 300).
I had to subtract y-axis origin of keyboard from device height, to come to correct value.
- (void)keyboardWillShow:(NSNotification *)notification
NSDictionary *info = [aNotification userInfo];
CGRect keyboardFrame = [info[UIKeyboardFrameBeginUserInfoKey] CGRectValue];
keyboardFrame = [info[UIKeyboardFrameEndUserInfoKey] CGRectValue];
keyboardFrame = [self.view convertRect:keyboardFrame fromView:nil];
CGFloat yPosition = keyboardFrame.origin.y;
NSInteger height = [UIScreen mainScreen].bounds.size.height - yPosition;
}
Works for me on iOS9 with iPhones and iPads in both orientations.
I created this table which contains both the heights of iPhone and iPad keyboards, both for landscape and portrait mode, both with the toolbar on and off.
I even explained how you can use these dimensions in your code here.
Note that you should only use these dimensions if you need to know the keyboard's dimension before you layout the view. Otherwise the solutions from the other answers work better.
© 2022 - 2024 — McMap. All rights reserved.