What is the height of iPhone's onscreen keyboard?
Asked Answered
K

7

87

The height in portrait and the height in landscape measured in points.

Kareenkarel answered 1/7, 2012 at 17:49 Comment(4)
Here you can find all the dimension of an iPhone: iPhone dimensionsShrieval
Do not use that last "iPhone dimensions" link ! It's from 2011, before iOS 8 came along, and allowed the onscreen keyboard height to vary.Tiaratibbetts
@MikeGledhill onscreen keyboard height vary even before iOS 8.Devoirs
Related: If you are looking for the actual values of keyboard heights (e.g. for debug purposes), see iOS foreign language keyboard heights?Trifocals
K
30

Keyboard height is 216pts for portrait mode and 162pts for Landscape mode.

Source

Kareenkarel answered 1/7, 2012 at 17:49 Comment(10)
If you're going to post self-answered questions, at least give decent answers. The size of the keyboard should be obtained from the notification object, not a hard-coded value taken from some website. See herePicofarad
@Picofarad Have a look at: #4214378. I only posted this question, because someone gave an answer with the height for iPhone and I figured there should be a question for iPhone as well.Kareenkarel
@Picofarad The question isn't how to find the height programmatically. This question is not just useful to programmers, but also to graphics designers and such. The answer you're asking for belongs to another question. In fact, there is another question with that answer: https://mcmap.net/q/153529/-ipad-keyboard-dimensionsKareenkarel
@ErikB Be careful. The keyboard size depends on the language and type of keyboard.Decal
@Decal I know. We've been over this in the iPad version of the question. I personally will probably not use these measurements for anything. I just made an iPhone version of the question and posted this answer: https://mcmap.net/q/153528/-what-is-the-height-of-ipad-39-s-onscreen-keyboardKareenkarel
For your own sake, NEVER use the hard-coded value! As a strong example, a user may be using the split (two-part) keyboard, which doesn't care much about height.Kikelia
@ErikB I think your answer could be more accurate if you rephrased your question (or this answer) to limit the scope to the standard English keyboard height instead of general "keyboard height."Wellworn
In iOS 8, its 224.0. Better use Notification!Chloechloette
This answer is wrong. You CANNOT assume the keyboard will be a specific height - you MUST measure it. See my screenshot, elsewhere in this thread, to see why you can't use a fixed height.Tiaratibbetts
This answer is lazy and dangerous. Please get keyboard height from the UIKeyboardDidShowNotification, as the actual value tends to change between iOS major release versions. See Ken's answer below.Glazer
C
215

I used the following approach for determining the frame of the keyboard in iOS 7.1.

In the init method of my view controller, I registered for the UIKeyboardDidShowNotification:

NSNotificationCenter *center = [NSNotificationCenter defaultCenter];
[center addObserver:self selector:@selector(keyboardOnScreen:) name:UIKeyboardDidShowNotification object:nil];

Then, I used the following code in keyboardOnScreen: to gain access to the frame of the keyboard. This code gets the userInfo dictionary from the notification and then accesses the NSValue associated with UIKeyboardFrameEndUserInfoKey. You can then access the CGRect and convert it to the coordinates of the view of your view controller. From there, you can perform any calculations you need based on that frame.

-(void)keyboardOnScreen:(NSNotification *)notification 
 {
        NSDictionary *info  = notification.userInfo;
        NSValue      *value = info[UIKeyboardFrameEndUserInfoKey];

        CGRect rawFrame      = [value CGRectValue];
        CGRect keyboardFrame = [self.view convertRect:rawFrame fromView:nil];

        NSLog(@"keyboardFrame: %@", NSStringFromCGRect(keyboardFrame));
 }

Swift

And the equivalent implementation with Swift:

NotificationCenter.default.addObserver(self, selector: #selector(keyboardDidShow), name: UIResponder.keyboardDidShowNotification, object: nil)


@objc
func keyboardDidShow(notification: Notification) {
    guard let info = notification.userInfo else { return }
    guard let frameInfo = info[UIResponder.keyboardFrameEndUserInfoKey] as? NSValue else { return }
    let keyboardFrame = frameInfo.cgRectValue
    print("keyboardFrame: \(keyboardFrame)")
}
Cosh answered 28/3, 2014 at 17:14 Comment(7)
While it seems a bit involved this is certainly better than hard-coding the keyboard size. Correct answer.Pettus
Voted this answer and down-voted the question. OP definitely doesn't know what he's talking about. Be careful guys! This answer is the correct one!Sapphire
one complement for Landscape: if (SYSTEM_VERSION_LESS_THAN(@"8.0")) { _keyboardHeight = keyboardFrame.size.width; } else { _keyboardHeight = keyboardFrame.size.height; }Repugn
Is there a way to do this without having the keyboard appear? In my case, I want to have something behind the keyboard with the same height (similar to how messengers have things)Southerner
@Sapphire There's no need to vote down the question; people will most likely search for this, and they will now see this answer .. so all is good :)Danais
Be careful... iPhone X height is 291 in portrait mode to accommodate for the emoticon and Siri icons. So, if you use the keyboard height to adjust a tableview.contentInset, then you need to subtract the distance between your tableview's bottom edge and the bottom of the window.Codger
i am really confused for keyboard extension . I am implementing custom keyboard and have added this Notification observer of UIResponder.keyboardWillShowNotification in viewWillAppear but it never get called . How to get height to build custom keyboard?Norma
T
46

Do remember that, with iOS 8, the onscreen keyboard's size can vary. Don't assume that the onscreen keyboard will always be visible (with a specific height) or invisible.

Now, with iOS 8, the user can also swipe the text-prediction area on and off... and when they do this, it would kick off an app's keyboardWillShow event again.

This will break a lot of legacy code samples, which recommended writing a keyboardWillShow event, which merely measures the current height of the onscreen keyboard, and shifting your controls up or down on the page by this (absolute) amount.

enter image description here

In other words, if you see any sample code, which just tells you to add a keyboardWillShow event, measure the keyboard height, then resize your controls' heights by this amount, this will no longer always work.

In my example above, I used the sample code from the following site, which animates the vertical constraints constant value.

Practicing AutoLayout

In my app, I added a constraint to my UITextView, set to the bottom of the screen. When the screen first appeared, I stored this initial vertical distance.

Then, whenever my keyboardWillShow event gets kicked off, I add the (new) keyboard height to this original constraint value (so the constraint resizes the control's height).

enter image description here

Yeah. It's ugly.

And I'm a little annoyed/surprised that Xcode 6's horribly-painful AutoLayout doesn't just allow us to attach the bottoms of controls to either the bottom of the screen, or the top of onscreen keyboard.

Perhaps I'm missing something.

Other than my sanity.

Tiaratibbetts answered 6/1, 2015 at 12:28 Comment(5)
actually, keyboard size always vary, even on the older iOS versions. e.g. check handwriting Chinese languageDevoirs
Ooops, didn't realise that. I just found it odd that so many "keyboardWillShow" samples always assumed that this event would just get called once when the keyboard appeared or disappeared.. with no functionality to cope with the keyboard staying onscreen, but changing its size.Tiaratibbetts
@MikeGledhill I think you shall use "UIKeyboardWillChangeFrameNotification" for the change of frame due to rotation or showing (/hiding) the text prediction bar...Hygro
I just tested this with my app. The UIKeyboardWillChangeFrameNotification always gets called ASWELL AS the keyboardWillShow event, whenever I either make the keyboard appear or turn the text prediction area on or off. So it doesn't really help much. My point is... users need to be a little careful with these events, that's all.Tiaratibbetts
Why doesn't this have a lot more attention?Cheslie
K
30

Keyboard height is 216pts for portrait mode and 162pts for Landscape mode.

Source

Kareenkarel answered 1/7, 2012 at 17:49 Comment(10)
If you're going to post self-answered questions, at least give decent answers. The size of the keyboard should be obtained from the notification object, not a hard-coded value taken from some website. See herePicofarad
@Picofarad Have a look at: #4214378. I only posted this question, because someone gave an answer with the height for iPhone and I figured there should be a question for iPhone as well.Kareenkarel
@Picofarad The question isn't how to find the height programmatically. This question is not just useful to programmers, but also to graphics designers and such. The answer you're asking for belongs to another question. In fact, there is another question with that answer: https://mcmap.net/q/153529/-ipad-keyboard-dimensionsKareenkarel
@ErikB Be careful. The keyboard size depends on the language and type of keyboard.Decal
@Decal I know. We've been over this in the iPad version of the question. I personally will probably not use these measurements for anything. I just made an iPhone version of the question and posted this answer: https://mcmap.net/q/153528/-what-is-the-height-of-ipad-39-s-onscreen-keyboardKareenkarel
For your own sake, NEVER use the hard-coded value! As a strong example, a user may be using the split (two-part) keyboard, which doesn't care much about height.Kikelia
@ErikB I think your answer could be more accurate if you rephrased your question (or this answer) to limit the scope to the standard English keyboard height instead of general "keyboard height."Wellworn
In iOS 8, its 224.0. Better use Notification!Chloechloette
This answer is wrong. You CANNOT assume the keyboard will be a specific height - you MUST measure it. See my screenshot, elsewhere in this thread, to see why you can't use a fixed height.Tiaratibbetts
This answer is lazy and dangerous. Please get keyboard height from the UIKeyboardDidShowNotification, as the actual value tends to change between iOS major release versions. See Ken's answer below.Glazer
H
17

version note: this is no longer value in iOS 9 & 10, as they support custom keyboard sizes.

This depends on the model and the QuickType bar:

enter image description here

http://www.idev101.com/code/User_Interface/sizes.html

Histo answered 23/3, 2015 at 15:1 Comment(1)
version note: this is no longer value in iOS 9 & 10, as they support custom keyboard sizes.Maleficence
D
11

The keyboard height depends on the model, the QuickType bar, user settings... The best approach is calculate dinamically:

Swift 3.0

var heightKeyboard : CGFloat?

override func viewWillAppear(_ animated: Bool) {
    super.viewWillAppear(animated)
    NotificationCenter.default.addObserver(self, selector: #selector(self.keyboardShown(notification:)), name: NSNotification.Name.UIKeyboardDidShow, object: nil)
}

func keyboardShown(notification: NSNotification) {
   if let infoKey  = notification.userInfo?[UIKeyboardFrameEndUserInfoKey],
       let rawFrame = (infoKey as AnyObject).cgRectValue {
       let keyboardFrame = view.convert(rawFrame, from: nil)
       self.heightKeyboard = keyboardFrame.size.height
       // Now is stored in your heightKeyboard variable
   }
}
Depolarize answered 12/10, 2016 at 12:1 Comment(0)
H
10

I can't find latest answer, so I checked it with all iOS 11.0 Simulators:

iOS 11 Device Screen Height Portrait Landscape
iPhone 4s 480.0 216.0 162.0
iPhone 5, iPhone 5s, iPhone SE 568.0 216.0 162.0
iPhone 6, iPhone 6s, iPhone 7, iPhone 8, iPhone X 667.0 216.0 162.0
iPhone 6 plus, iPhone 7 plus, iPhone 8 plus 736.0 226.0 162.0
iPad 5th generation, iPad Air, iPad Air 2, iPad Pro 9.7, iPad Pro 10.5, iPad Pro 12.9 1024.0 265.0 353.0
Hissing answered 11/10, 2017 at 5:37 Comment(0)
J
2

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.

enter image description here

Joubert answered 26/12, 2020 at 16:57 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.