2012-12-16 27 views
1

我有一個自定義的繼承UIView類與UITableView作爲其唯一的子視圖。當鍵盤顯示爲將桌面視圖的contentInsetscrollIndicatorInsets調整爲鍵盤高度時,我試圖模仿UITableViewController的正常功能。這是我的方法,當鍵盤從我的自定義UIView類中沒有顯示,被稱爲:表視圖不能正確調整到鍵盤

- (void)keyboardDidShow:(NSNotification*)notification 
{ 
    NSDictionary* info = [notification userInfo]; 
    CGSize kbSize = [[info objectForKey:UIKeyboardFrameEndUserInfoKey] CGRectValue].size; 
    _tableView.contentInset = UIEdgeInsetsMake(0.0, 0.0, kbSize.height, 0.0); 
    _tableView.scrollIndicatorInsets = _tableView.contentInset; 
} 

此作品在一定程度上,但仍有鍵盤的一些重疊到表視圖出於某種原因由大約十個左右像素。

Keyboard Overlap

我想它是與沒有考慮到一些其他的屏幕幾何形狀的,但我不明白怎麼會是。鍵盤的高度應該正是我所需要的,因爲tableView一直延伸到屏幕的底部。有任何想法嗎?

回答

1

更改tableView.frame.size.height以考慮鍵盤。

當鍵盤顯示時,降低高度, 未顯示時,增加高度。

指這個,如果你要考慮鍵盤的高度,所有的可能性http://www.idev101.com/code/User_Interface/sizes.html

不要亂用contentInset和scrollIndicatorInsets。只需設置frameSize就可以幫你處理這些問題。

這是你的方法應該如何

- (void)keyboardDidShow:(NSNotification*)notification 
{ 
    NSDictionary* info = [notification userInfo]; 
    CGSize kbSize = [[info objectForKey:UIKeyboardFrameEndUserInfoKey] CGRectValue].size; 
    CGRect rect = _tableView.frame; 
    rect.size.height = _tableView.frame.size.height - kbSize.height; 
    _tableView.frame = rect; 
} 

- (void)keyboardWillHide:(NSNotification*)notification 
{ 
    NSDictionary* info = [notification userInfo]; 
    CGSize kbSize = [[info objectForKey:UIKeyboardFrameEndUserInfoKey] CGRectValue].size; 
    CGRect rect = _tableView.frame; 
    rect.size.height = _tableView.frame.size.height + kbSize.height; 
    _tableView.frame = rect; 
} 

我已經使用這段代碼的類似的功能。所以如果它仍然不能正常工作,那還有其他問題。

+0

我正在舉例說明關於**管理鍵盤**的Apple文檔。查看位於Keyboard_部分下的_Moving內容。你看到Apple建議使用插圖。不過,我確實給你的解決方案一個鏡頭。同樣的問題,但現在我可以清楚地看到鍵盤返回的高度不夠。下面是一個組合框,顯示了我的框架在按鍵盤返回的高度減去它時的樣子。有趣的是,返回的高度是216,這正是蘋果所說的。我有點失落。 – Anna

+0

這裏是我的複合:[http://i.stack.imgur.com/dDt4w.png](http://i.stack.imgur.com/dDt4w.png) – Anna

+0

你可以發佈你試過的代碼嗎? –

0

我很好奇爲什麼這不適合你,因爲我基本上是一樣的東西,它爲我工作。我只能看到一個區別,因爲我不訪問'_tableView',而是確保我總是使用getter和setter。

這是我做的,那是工作。

- (void)keyboardDidShow:(NSNotification *)keyboardNotification 
{ 
    NSDictionary *info = [keyboardNotification userInfo]; 
    CGSize keyboardSize = [[info objectForKey:UIKeyboardFrameEndUserInfoKey] CGRectValue].size; 

    CGFloat newBottomInset = 0.0; 

    UIEdgeInsets contentInsets; 
    if (UIDeviceOrientationIsPortrait([[UIApplication sharedApplication] statusBarOrientation])) { 
     newBottomInset = keyboardSize.height; 
    } else { 
     newBottomInset = keyboardSize.width; 
    } 

    contentInsets = UIEdgeInsetsMake(0.0, 0.0, newBottomInset, 0.0); 
    self.tableView.contentInset = contentInsets; 
    self.tableView.scrollIndicatorInsets = contentInsets; 
} 

請注意,我的應用程序允許裝置轉動,當這種情況發生的使用值需要在鍵盤的寬度,因爲該值是相對於縱向方向,這引起了我的困惑小時。

希望self.tableView訪問將有所作爲。