【问题标题】:Check for split keyboard检查拆分键盘
【发布时间】:2011-12-12 04:05:18
【问题描述】:

正如许多人所知,iOS 5 引入了用于拇指打字的灵巧拆分键盘。不幸的是,我有一些 UI 依赖于正常的全屏键盘布局。我的一个视图控制器向用户展示了一个文本输入表,如果他们单击一个将被键盘覆盖的文本字段,它会随着键盘一起向上滑动。拆分键盘不需要此操作。

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

谢谢!

【问题讨论】:

标签: ios ipad cocoa-touch ios5 keyboard


【解决方案1】:

这是适用于 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
     }
}];

【讨论】:

  • 很好,但不是检查键盘是否与当前视图的框架相交,而是检查它是否在屏幕上:CGRect screenRect = [[UIScreen mainScreen] bounds]; if (CGRectIntersectsRect(keyboardEndFrame, screenRect)) ...
  • @MasonLee 根据您的评论更新。谢谢!
【解决方案2】:

UIKeyboardFrameChangedByUserInteractionkey 在键盘拆分时不会一直返回 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】:

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

    如果键盘对接,UIKeyboardWillShowNotification 将被提升,以下情况为真:

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

    如果一个键盘被取消停靠,UIKeyboardWillHideNotification 将被提升,并且上面的陈述也将是正确的。

    使用这些信息对我的用户界面进行编码已经足够了。

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

    【讨论】:

    • UIKeyboardFrameChangedByUserInteraction 的值在 iOS7 中不正确
    【解决方案4】:

    当键盘出现或改变其位置(UIKeyboardWillShowNotificationUIKeyboardWillChangeFrameNotification)时发布的通知包含一个带有键盘框架的userInfo 字典(UIKeyboardFrameEndUserInfoKey),允许您定位您的 UI 元素正确,取决于键盘的实际大小和位置。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-03-04
      • 2015-10-31
      • 2012-07-06
      • 1970-01-01
      • 1970-01-01
      • 2011-05-31
      • 2018-08-07
      相关资源
      最近更新 更多