检查分离键盘

许多人都知道iOS 5引入了一个精巧的分离式键盘来进行拇指input。 不幸的是,我有一些依赖于正常的全屏键盘布局的用户界面。 我的一个视图控制器给用户提供了一个文本input表,如果他们点击到键盘覆盖的文本字段,它会随着键盘滑动。 分离键盘不需要此操作。

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

谢谢!

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

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

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

如果键盘未连接,则会引发UIKeyboardWillHideNotification ,上面的语句也是如此。

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

注意:这可能违反了苹果的准则,我不确定。

这是与iPad分离键盘(最初来自Zeeshan评论链接的博客)的解决scheme,

 [[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 } }]; 

键盘分割时, 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键在键盘分割时得到通知。

希望这可以帮助!

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