2011-10-20 62 views
15

如你們許多人所知,iOS 5引入了一個精巧的拆分鍵盤來進行拇指輸入。不幸的是,我有一些UI依賴於普通的全屏鍵盤佈局。我的一個視圖控制器爲用戶提供了一個文本輸入表,如果他們點擊一個鍵盤覆蓋的textField,它會隨着鍵盤一起滑動。分離鍵盤不需要此操作。檢查拆分鍵盤

有沒有辦法在彈出之前檢查哪個鍵盤佈局正在使用?

謝謝!

+0

將不勝感激你已是如何解決這個更新! –

+0

我試着回答這個問題在[這裏] [1] [1]:http://stackoverflow.com/a/17567217/887325爲'UIKeyboardFrameChangedByUserInteraction'is – Bimawa

回答

17

當鍵盤停靠時,UIKeyboardWillShowNotification將被擡起。如果鍵盤分離或未連接,則不會引發鍵盤通知。

如果鍵盤被停靠,UIKeyboardWillShowNotification將提高,而下面將是真實的:

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

如果鍵盤被斷開時,UIKeyboardWillHideNotification將提高,而上面的語句也將是如此。

使用這些信息已經足以讓我編寫我的用戶界面。

注意:這可能違反了Apple的準則,我不確定。

+0

值不iOS7 – Daniel

1

當鍵盤出現或改變其位置(UIKeyboardWillShowNotificationUIKeyboardWillChangeFrameNotification)所發佈的通知包含userInfo字典與鍵盤(UIKeyboardFrameEndUserInfoKey)的框架,讓您準確定位你的UI元素,這取決於實際尺寸和鍵盤的位置。

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鍵得到通知時,鍵盤分割。

希望這會有所幫助!

+5

正確_This爲我工作[[iPad的拆分鍵盤和(失蹤)通知](http://adevelopingstory.com/blog/2012/05/the-ipad-split-keyboard-and-missing-notifications.html), – Zeeshan

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 
    } 
}]; 
+1

偉大,而不是檢查鍵盤是否與當前視圖的框架相交,只需檢查它是否在屏幕上:CGRect screenRect = [[UIScreen mainScreen] bounds];如果(CGRectIntersectsRect(keyboardEndFrame,screenRect))... –

+0

@MasonLee根據您的評論更新。謝謝! – Robert