Reputation: 16952
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?
Upvotes: 4
Views: 779
Reputation: 16952
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;
}
Upvotes: 5