简体   繁体   中英

Get iOS Keyboard Height, when Hardware Keyboard is Attached

When a hardware keyboard is attached to an iOS device (iPad), there is still a smaller part of the software keyboard visible: the toolbar with the word suggestions and the undo and redo buttons.

Originally I expected that the documented method for inferring the keyboard height (see https://developer.apple.com/library/ios/documentation/StringsTextFonts/Conceptual/TextAndWebiPhoneOS/KeyboardManagement/KeyboardManagement.html ) would - in this case - just give the height of this bar. However, instead the reported height is still the height of the full software keyboard (although only a part is visible).

How can we get the height of the visible software keyboard, when a hardware keyboard is attached?

An answer to this question is part of Sarah Elans answer to her question " How to reliably detect if an external keyboard is connected on iOS 9? " (I don't see the question here as a duplicate, although the other answer covers part of it (also the specific formula is missing)):

One can get the visible height of the software keyboard by inspecting origin.y (instead of size.height), then subtract that value from the window's height, that is

// Called when the UIKeyboardDidShowNotification is sent.
- (void)keyboardWasShown:(NSNotification*)aNotification
{
    NSDictionary* info = [aNotification userInfo];

    // Get the size of the keyboard.
    NSValue* keyboardFrameValue     = [info objectForKey:UIKeyboardFrameEndUserInfoKey];
    CGRect keyboardRectWrtScreen    = [keyboardFrameValue CGRectValue];

    keyboardWidth = keyboardRectWrtScreen.size.width;
    keyboardHeight = [[[self view] window] frame].size.height - keyboardRectWrtScreen.origin.y;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM