2012-12-16 82 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访问将有所作为。