What is the height of iPhone's onscreen keyboard?

120,335

Solution 1

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

Source

Solution 2

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)")
}

Solution 3

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.

Solution 4

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

Solution 5

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
   }
}
Share:
120,335
Erik B
Author by

Erik B

Professional iPhone and iPad developer. I have some of Sweden's most downloaded apps on my resume. #SOreadytohelp

Updated on September 19, 2021

Comments

  • Erik B
    Erik B over 2 years

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

  • jrturton
    jrturton almost 12 years
    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 here
  • Erik B
    Erik B almost 12 years
    @jrturton Have a look at: stackoverflow.com/questions/4213878/…. 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.
  • Erik B
    Erik B almost 12 years
    @jrturton 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: stackoverflow.com/a/7302291/310121
  • Gabriel
    Gabriel almost 12 years
    @ErikB Be careful. The keyboard size depends on the language and type of keyboard.
  • Erik B
    Erik B almost 12 years
    @Gabriel 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: stackoverflow.com/a/11275435/310121
  • Timo
    Timo over 10 years
    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.
  • n13
    n13 almost 10 years
    While it seems a bit involved this is certainly better than hard-coding the keyboard size. Correct answer.
  • superarts.org
    superarts.org almost 10 years
    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!
  • LiangWang
    LiangWang almost 10 years
    one complement for Landscape: if (SYSTEM_VERSION_LESS_THAN(@"8.0")) { _keyboardHeight = keyboardFrame.size.width; } else { _keyboardHeight = keyboardFrame.size.height; }
  • Stonz2
    Stonz2 over 9 years
    @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."
  • Developer
    Developer over 9 years
    In iOS 8, its 224.0. Better use Notification!
  • Mike Gledhill
    Mike Gledhill over 9 years
    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.
  • ReDetection
    ReDetection over 9 years
    actually, keyboard size always vary, even on the older iOS versions. e.g. check handwriting Chinese language
  • Mike Gledhill
    Mike Gledhill over 9 years
    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.
  • Abdalrahman Shatou
    Abdalrahman Shatou over 9 years
    @MikeGledhill I think you shall use "UIKeyboardWillChangeFrameNotification" for the change of frame due to rotation or showing (/hiding) the text prediction bar...
  • Mike Gledhill
    Mike Gledhill over 9 years
    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.
  • rfoo
    rfoo over 9 years
    Why doesn't this have a lot more attention?
  • John
    John about 9 years
    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)
  • Ja͢ck
    Ja͢ck almost 9 years
    @superarts.org 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 :)
  • Alexander
    Alexander over 8 years
    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.
  • Raptor
    Raptor almost 7 years
    version note: this is no longer value in iOS 9 & 10, as they support custom keyboard sizes.
  • Brainware
    Brainware over 6 years
    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.
  • Amit Verma
    Amit Verma over 4 years
    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?