Check for split keyboard
Asked Answered
F

4

15

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 of my view controllers presents the user with a text entry sheet, and if they click into a textField that would be covered by the keyboard, it slides up along with the keyboard. This action is unnecessary with the split keyboard.

Is there a way to check which keyboard layout is in use before it pops up?

Thanks!

Finke answered 20/10, 2011 at 21:59 Comment(2)
Would appreciate an update on how youve solved this!Lamonica
I try answer on this question in [here][1] [1]: https://mcmap.net/q/822729/-ipad-split-keyboardMinta
S
18

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.

Sethrida answered 9/1, 2012 at 8:16 Comment(1)
the value for UIKeyboardFrameChangedByUserInteractionis not correct at iOS7Photochronograph
E
9

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
     }
}];
Ergograph answered 21/11, 2012 at 14:42 Comment(2)
Great, but instead of checking that the keyboard intersects the current view's frame, just check if it's on screen: CGRect screenRect = [[UIScreen mainScreen] bounds]; if (CGRectIntersectsRect(keyboardEndFrame, screenRect)) ...Jun
@MasonLee Updated as per your comment. Thanks!Ergograph
K
5

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!

Khedive answered 11/7, 2012 at 6:40 Comment(0)
L
1

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.

Luncheon answered 21/10, 2011 at 1:47 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.