2014-09-23 78 views
7

我有一個用戶界面,當顯示鍵盤時需要調整位置。iOS8 UIKeyboardWillShowNotification第三方鍵盤高度

以下是用來檢測時,會顯示一個鍵盤:

[[NSNotificationCenter defaultCenter] addObserver:self 
             selector:@selector(keyboardWillShow:) 
              name:UIKeyboardWillShowNotification 
              object:nil]; 

注:我已經試過兩UIKeyboardWillShowNotification和UIKeyboardDidShowNotification

- (void)keyboardWillShow:(NSNotification *)n { 
    if (isKeyboardShowing) 
    return; 

NSDictionary* userInfo = [n userInfo]; 

// get the size of the keyboard 
CGSize keyboardSize = [[userInfo objectForKey:UIKeyboardFrameBeginUserInfoKey] CGRectValue].size; 


// Animate keyboard shift 
[self.view layoutIfNeeded]; 

[UIView animateWithDuration:0.2f animations:^{ 
    // some animation here 
} completion:^(BOOL finished) { 
    if (finished) 
     isKeyboardShowing = YES; 
}]; 

對於自定義鍵盤上鍵盤尺寸返回{320 ,0}。由於鍵盤現在可以有不同的高度,因此當鍵盤出現時,我無法使用靜態值來更改UI。

這是ios8的問題嗎?有沒有其他方法來動態獲得鍵盤高度?

編輯:這是用戶信息詞典:

{name = UIKeyboardDidShowNotification; userInfo = { 
    UIKeyboardAnimationCurveUserInfoKey = 7; 
    UIKeyboardAnimationDurationUserInfoKey = "0.25"; 
    UIKeyboardBoundsUserInfoKey = "NSRect: {{0, 0}, {320, 0}}"; 
    UIKeyboardCenterBeginUserInfoKey = "NSPoint: {160, 568}"; 
    UIKeyboardCenterEndUserInfoKey = "NSPoint: {160, 568}"; 
    UIKeyboardFrameBeginUserInfoKey = "NSRect: {{0, 568}, {320, 0}}"; 
    UIKeyboardFrameEndUserInfoKey = "NSRect: {{0, 568}, {320, 0}}"; 
}} 

在此先感謝。

回答

11

使用

CGSize keyboardSize = [[userInfo objectForKey:UIKeyboardFrameEndUserInfoKey] CGRectValue].size; 

讓鍵盤的實際大小