检查分离键盘

Eva*_*vis 15 keyboard cocoa-touch ipad ios ios5

很多人都知道iOS 5引入了一个用于拇指键入的光滑分离键盘.不幸的是,我有一些依赖于普通全屏键盘布局的UI.我的一个视图控制器向用户显示一个文本输入表,如果他们点击一个将被键盘覆盖的textField,它会随键盘一起向上滑动.拆分键盘不需要此操作.

有没有办法在弹出之前检查哪个键盘布局正在使用?

谢谢!

Dav*_*son 17

当键盘停靠时,UIKeyboardWillShowNotification会被抬起.如果键盘已拆分或未对接,则不会引发键盘通知.

如果键盘停靠,UIKeyboardWillShowNotification将会引发,以下情况将成立:

[[[notification userInfo] valueForKey:@"UIKeyboardFrameChangedByUserInteraction"] intValue] == 1
Run Code Online (Sandbox Code Playgroud)

如果键盘未对接,UIKeyboardWillHideNotification将被引发,并且上述语句也将为真.

使用这些信息足以让我编写用户界面代码.

注意:这可能违反了Apple的指导方针,我不确定.


Rob*_*ert 9

这是与iPad分离键盘一起使用的解决方案(最初来自Zeeshan评论中链接的博客)

[[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
     }
}];
Run Code Online (Sandbox Code Playgroud)


小智 5

UIKeyboardFrameChangedByUserInteraction 当键盘分割时,键不会一直返回1.

以下是完整的用户信息在字典中的键值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}}";
}
Run Code Online (Sandbox Code Playgroud)

相反,您可以使用UIKeyboardCenterBeginUserInfoKeyUIKeyboardCenterEndUserInfoKey键在键盘分割时获得通知.

希望这可以帮助!