As many of you know iOS 5 introduced a slick split keyboard for thumb-typing. Unfortunately, I have some UI that is dependent on the normal full-screen keyboard layout. One
This is the solution which works with iPad split keyboards (originally from the blog linked in Zeeshan's comment)
[[NSNotificationCenter defaultCenter]
addObserverForName:UIKeyboardDidChangeFrameNotification
object:nil
queue:[NSOperationQueue mainQueue]
usingBlock:^(NSNotification * notification)
{
CGRect keyboardEndFrame =
[[notification.userInfo objectForKey:UIKeyboardFrameEndUserInfoKey] CGRectValue];
CGRect screenRect = [[UIScreen mainScreen] bounds];
if (CGRectIntersectsRect(keyboardEndFrame, screenRect))
{
// Keyboard is visible
}
else
{
// Keyboard is hidden
}
}];
When the keyboard is docked, UIKeyboardWillShowNotification
will be raised. If the keyboard is split or undocked, no keyboard notifications are raised.
If a keyboard is docked, UIKeyboardWillShowNotification
will be raised, and the following will be true:
[[[notification userInfo] valueForKey:@"UIKeyboardFrameChangedByUserInteraction"] intValue] == 1
If a keyboard is undocked, UIKeyboardWillHideNotification
will be raised, and the above statement will also be true.
Using this information has been adequate for me to code my user interface.
Note: this might be a violation of Apple's guidelines, I'm not sure.
The notifications that are posted when the keyboard appears or changes its position (UIKeyboardWillShowNotification
, UIKeyboardWillChangeFrameNotification
) contain a userInfo
dictionary with the frame of the keyboard (UIKeyboardFrameEndUserInfoKey
) that allows you to position your UI elements correctly, depending on the actual size and location of the keyboard.
UIKeyboardFrameChangedByUserInteraction
key does not return 1 all the time when keyboard splits.
Below is the full user info dictionary key values on UIKeyboardDidShowNotification
/ UIKeyboardDidHideNotification
.
2012-07-11 11:52:44.701 Project[3856:707] keyboardDidShow: {
UIKeyboardBoundsUserInfoKey = "NSRect: {{0, 0}, {1024, 352}}";
UIKeyboardCenterBeginUserInfoKey = "NSPoint: {512, 944}";
UIKeyboardCenterEndUserInfoKey = "NSPoint: {512, 592}";
UIKeyboardFrameBeginUserInfoKey = "NSRect: {{-352, 0}, {352, 1024}}";
UIKeyboardFrameChangedByUserInteraction = 0;
UIKeyboardFrameEndUserInfoKey = "NSRect: {{0, 0}, {352, 1024}}";
}
2012-07-11 11:52:45.675 Project[3856:707] keyboardDidHide: {
UIKeyboardBoundsUserInfoKey = "NSRect: {{0, 0}, {1024, 352}}";
UIKeyboardCenterBeginUserInfoKey = "NSPoint: {512, 592}";
UIKeyboardCenterEndUserInfoKey = "NSPoint: {512, 944}";
UIKeyboardFrameBeginUserInfoKey = "NSRect: {{0, 0}, {352, 1024}}";
UIKeyboardFrameChangedByUserInteraction = 0;
UIKeyboardFrameEndUserInfoKey = "NSRect: {{-352, 0}, {352, 1024}}";
}
Instead you can use UIKeyboardCenterBeginUserInfoKey
or UIKeyboardCenterEndUserInfoKey
keys to get notified when keyboard splits.
Hope this helps!