Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

iOS 8 get keyboard height

Tags:

ios

iphone

ios8

I'm having trouble reliably getting the actual height of the keyboard. I'm adding a custom toolbar that is meant to be placed directly above the keyboard. To do so with layout constraints, I've added a static space constraint between the bottom of the screen and the bottom of the toolbar. When the keyboard is displayed, I modify this constraint to be the height of the keyboard, which varies quite a bit in iOS 8.

To resize the spacer, I currently use the following method that gets fired on UIKeyboardDidShowNotification

-(void)keyboardDidShow:(NSNotification*)notification
{
   CGFloat height = [[notification.userInfo objectForKey:UIKeyboardFrameBeginUserInfoKey] CGRectValue].height;

   self.shimConstraint.constant = height;
   [self.view layoutIfNeeded];
}

This does resize the constraint correctly sometimes, but often the keyboard height returned is entirely incorrect. What is the best way to reliably get the height of the keyboard once it is displayed?

EDIT: The application allows the user to switch between a number of different input fields. Some of them have autocorrect disabled, so the autocorrect bar may or may not be hidden, and incorrect heights are returned when switching fields.

like image 936
James Avatar asked Sep 30 '14 00:09

James


People also ask

What is the height of the IOS keyboard?

The top of the keyboard needs to be about 2 inches from the bottom of the *device* as it is held. Prior to the iPhone X, this is easy because all devices used the exact same bezel insets, so it's 216 pts from the bottom of the screen.

How do I change the keyboard height on my iPhone?

On your iPhone open the "Settings" app, scroll down to find the "Display & Brightness" option, then tap it. Afterwards scroll down to the bottom and tap "View" under "Display Zoom". Here you'll see two options, a "Standard" one and a "Zoomed" one.

How do I get the height of a swift key?

Swift. You can get the keyboard height by subscribing to the UIKeyboardWillShowNotification notification.


2 Answers

The way you are doing it is correct but you might want to use UIKeyboardFrameEndUserInfoKey instead of UIKeyboardFrameBeginUserInfoKey. The end user key is the frame of the keyboard when it is done animating and on the screen, thus you know it will be that frame, while the begin key might not always match what the keyboard frame will be when it is shown.

There might also need to be extra considerations on orientation of the device, I have not looked into that, but for portrait, it should work.

like image 155
Kris Gellci Avatar answered Sep 22 '22 02:09

Kris Gellci


The correct way to do this is to use the inputAccesorryView property of the UITextField.

The inputAccessoryView is automatically added above the keyboard regardless of keyboard size.

As the documentation states:

The default value of this property is nil. Assigning a view to this property causes that view to be displayed above the standard system keyboard (or above the custom input view if one is provided) when the text field becomes the first responder. For example, you could use this property to attach a custom toolbar to the keyboard

See more information here https://developer.apple.com/library/ios/documentation/StringsTextFonts/Conceptual/TextAndWebiPhoneOS/InputViews/InputViews.html

like image 37
Jason Kirchner Avatar answered Sep 24 '22 02:09

Jason Kirchner