UIKeyboardBoundsUserInfoKey is deprecated, what to use instead?

前端 未结 9 1759
我在风中等你
我在风中等你 2020-12-02 04:56

I\'m working on an iPad app using 3.2 sdk. I\'m dealing with obtaining the keyboard size to prevent my textfields from hidding behind it.

I\'m getting a Warning in

9条回答
  •  伪装坚强ぢ
    2020-12-02 05:21

    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;
    

提交回复
热议问题