Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get UIKeyboard size with iOS

Is there some way to get UIKeyboard size programmatically. 216.0f height and 162.0f height in landscape.

Following seem to be deprecated. Is there some way that works without any warning in both 3.0 iPhone OS SDK and 4.0 iPhone OS SDK to do this..

CGSize keyBoardSize = [[[note userInfo]                         objectForKey:UIKeyboardBoundsUserInfoKey] CGRectValue].size; 
like image 689
Tharindu Madushanka Avatar asked Aug 23 '10 10:08

Tharindu Madushanka


People also ask

What is the height of keyboard in IOS?

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.


2 Answers

You can get the keyboard size from the userInfo dictionary using the UIKeyboardFrameBeginUserInfoKey and the UIKeyboardFrameEndUserInfoKey instead.

These two keys return a NSValue instance containing a CGRect that holds the position and size of the keyboard at both the start and end points of the keyboard's show/hide animation.

Edit:

To clarify, the userInfo dictionary comes from an NSNotification instance. It's passed to your method that you register with an observer. For example,

- (void)someMethodWhereYouSetUpYourObserver {     // This could be in an init method.     [[NSNotificationCenter defaultCenter] addObserver:self                      selector:@selector(myNotificationMethod:)                      name:UIKeyboardDidShowNotification                      object:nil]; }  - (void)myNotificationMethod:(NSNotification*)notification {     NSDictionary* keyboardInfo = [notification userInfo];     NSValue* keyboardFrameBegin = [keyboardInfo valueForKey:UIKeyboardFrameBeginUserInfoKey];     CGRect keyboardFrameBeginRect = [keyboardFrameBegin CGRectValue]; } 

Edit 2:

Also, please don't forget to remove yourself as an observer in your dealloc method! This is to avoid a crash that would occur when the notification center tries to notify your object after its been freed.

like image 82
James Bedford Avatar answered Sep 20 '22 08:09

James Bedford


You should use the UIKeyboardWillChangeFrameNotification instead, because some international keyboards, like the Chinese keyboard, will change frames during use. Also make sure to convert the CGRect into the proper view, for landscape use.

//some method like viewDidLoad, where you set up your observer. [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillChange:) name:UIKeyboardWillChangeFrameNotification object:nil];  - (void)keyboardWillChange:(NSNotification *)notification {     CGRect keyboardRect = [notification.userInfo[UIKeyboardFrameEndUserInfoKey] CGRectValue];     keyboardRect = [self.view convertRect:keyboardRect fromView:nil]; //this is it! } 
like image 33
Jeffrey Sun Avatar answered Sep 20 '22 08:09

Jeffrey Sun