如何获取键盘的高度,而不需要通过通知?

- (CGFloat)visibleKeyboardHeight {

    

    UIWindow *keyboardWindow = nil;

    for (UIWindow *testWindow in [[UIApplication sharedApplication] windows]) {

        if(![[testWindow class] isEqual:[UIWindow class]]) {

            keyboardWindow = testWindow;

            break;

        }

    }

    

    for (__strong UIView *possibleKeyboard in [keyboardWindow subviews]) {

        if([possibleKeyboard isKindOfClass:NSClassFromString(@"UIPeripheralHostView")] || [possibleKeyboard isKindOfClass:NSClassFromString(@"UIKeyboard")])

            return possibleKeyboard.bounds.size.height;

    }

    

    return 0;

}


上面这段代码能计算当前的键盘的高度,如果没有键盘,返回的高度就为0

你可能感兴趣的:(如何获取键盘的高度,而不需要通过通知?)