iOS 9 adds a Shortcut Bar to the iOS 8 QuickType bar.
As part of this change, if you connect a bluetooth keyboard to an iPad, the keyboard is in a minimized Shortcut Bar only mode (which can be simulated by pressing command-k in the simulator).
I have code which gets the keyboard height using a method similar to the following:
CGRect keyboardFrame = [[info objectForKey:UIKeyboardFrameEndUserInfoKey] CGRectValue];
CGFloat keyboardHeight = keyboardFrame.size.height; // = 313
The problem is that when the keyboard is toggled between the expanded and collapsed state, the height remains the same, causing my view to appear in its old location:
Desired behavior:
(Notice how the red view is attached to the top of the keyboard)
Actual behavior:
What's the correct way to get the red view to be attached to the top of the keyboard?
The problem is that most code out there (including Apple's) ignores the fact that UIKeyboardFrameEndUserInfoKey is a CGRect and not a CGSize.
Here you see that only the keyboard height (
kbSize.height
) is being used. The origin of the rect is important, and should not be ignored.When the keyboard is visible, this is the rect that is reported:
When the keyboard is in Shortcut Bar only mode, this is the rect:
Notice how the majority of the keyboard is offscreen, yet it is still the same height.
To get the correct behavior, use CGRectIntersection with the view's bounds and the keyboard frame within that view:
For this same reason,
UIKeyboardFrameEndUserInfoKey
should be used as opposed toUIKeyboardFrameBeginUserInfoKey
.