UIKeyboardBoundsUserInfoKey is deprecated, what to use instead? UIKeyboardBoundsUserInfoKey is deprecated, what to use instead? ios ios

UIKeyboardBoundsUserInfoKey is deprecated, what to use instead?


I played with the previously offered solution but still had issues. Here's what I came up with instead:

    - (void)keyboardWillShow:(NSNotification *)aNotification {    [self moveTextViewForKeyboard:aNotification up:YES];}    - (void)keyboardWillHide:(NSNotification *)aNotification {        [self moveTextViewForKeyboard:aNotification up:NO];     }- (void) moveTextViewForKeyboard:(NSNotification*)aNotification up: (BOOL) up{NSDictionary* userInfo = [aNotification userInfo];// Get animation info from userInfoNSTimeInterval animationDuration;UIViewAnimationCurve animationCurve;CGRect keyboardEndFrame;[[userInfo objectForKey:UIKeyboardAnimationCurveUserInfoKey] getValue:&animationCurve];[[userInfo objectForKey:UIKeyboardAnimationDurationUserInfoKey] getValue:&animationDuration];[[userInfo objectForKey:UIKeyboardFrameEndUserInfoKey] getValue:&keyboardEndFrame];// Animate up or down[UIView beginAnimations:nil context:nil];[UIView setAnimationDuration:animationDuration];[UIView setAnimationCurve:animationCurve];CGRect newFrame = textView.frame;CGRect keyboardFrame = [self.view convertRect:keyboardEndFrame toView:nil];newFrame.origin.y -= keyboardFrame.size.height * (up? 1 : -1);textView.frame = newFrame;[UIView commitAnimations];}


From the documentation for UIKeyboardBoundsUserInfoKey:

The key for an NSValue object containing a CGRect that identifies the bounds rectangle of the keyboard in window coordinates. This value is sufficient for obtaining the size of the keyboard. If you want to get the origin of the keyboard on the screen (before or after animation) use the values obtained from the user info dictionary through the UIKeyboardCenterBeginUserInfoKey or UIKeyboardCenterEndUserInfoKey constants. Use the UIKeyboardFrameBeginUserInfoKey or UIKeyboardFrameEndUserInfoKey key instead.

Apple recommends implementing a convenience routine such as this (which could be implemented as a category addition to UIScreen):

+ (CGRect) convertRect:(CGRect)rect toView:(UIView *)view {    UIWindow *window = [view isKindOfClass:[UIWindow class]] ? (UIWindow *) view : [view window];    return [view convertRect:[window convertRect:rect fromWindow:nil] fromView:nil];}

to recover window-adjusted keyboard frame size properties.

I took a different approach, which involves checking the device orientation:

CGRect _keyboardEndFrame;[[notification.userInfo valueForKey:UIKeyboardFrameEndUserInfoKey] getValue:&_keyboardEndFrame];CGFloat _keyboardHeight = ([[UIDevice currentDevice] orientation] == UIDeviceOrientationPortrait || [[UIDevice currentDevice] orientation] == UIDeviceOrientationPortraitUpsideDown) ? _keyboardEndFrame.size.height : _keyboardEndFrame.size.width;


You simply use this code:

//NSVale *aValue = [info objectForKey:UIKeyboardBoundsUserInfoKey];//instead of Upper line we can use either next line or nextest line.//NSValue *aValue = [info objectForKey:UIKeyboardFrameEndUserInfoKey];NSValue *aValue = [info objectForKey:UIKeyboardFrameBeginUserInfoKey];