检查分离键盘

时间:2011-10-20 21:59:22

标签: ios ipad cocoa-touch ios5 keyboard

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

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

谢谢!

4 个答案:

答案 0 :(得分:17)

当键盘停靠时,将会引发UIKeyboardWillShowNotification。如果键盘被拆分或取消停靠,则不会引发键盘通知。

如果键盘已停靠,则会引发UIKeyboardWillShowNotification,并且以下情况属实:

[[[notification userInfo] valueForKey:@"UIKeyboardFrameChangedByUserInteraction"] intValue] == 1

如果键盘未对接,则会引发UIKeyboardWillHideNotification,上述声明也将为真。

使用此信息足以让我编写用户界面代码。

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

答案 1 :(得分: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
     }
}];

答案 2 :(得分: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}}";
}

相反,您可以使用UIKeyboardCenterBeginUserInfoKeyUIKeyboardCenterEndUserInfoKey键在键盘分割时收到通知。

希望这有帮助!

答案 3 :(得分:1)

键盘出现或更改其位置(UIKeyboardWillShowNotificationUIKeyboardWillChangeFrameNotification)时发布的通知包含带有键盘框架的userInfo字典(UIKeyboardFrameEndUserInfoKey)这使您可以正确定位UI元素,具体取决于键盘的实际大小和位置。