如何让UITableView向上滚动以容纳键盘?

问题描述:

任何人都可以解释如何让这些方法工作? 他们编译得很好,但他们什么都不做。如何让UITableView向上滚动以容纳键盘?

我想让我的tableView滚动,这样我的键盘很适合。

针对此问题发布的解决方案是重置contentSizes并订阅NSNotfication。这三种方法看起来像他们应该做我想要的,但没有任何反应。

[self.myTable setContentOffset:CGPointMake(0, -100) animated:YES]; 

[self.myTable selectRowAtIndexPath:indexPath animated:YES scrollPosition:UITableViewScrollPositionTop]; 

[self.myTable scrollToRowAtIndexPath:indexPath atScrollPosition:UITableViewScrollPositionTop animated:YES]; 

您可以使用setFrame做出的UITableView的大小屏幕尺寸的一半。

您发布的代码是相当不完整的,以调试您做错了什么,因为它只存在一些需要的部分。请查看这是否是您正在寻找的解决方案?

在你init方法中添加以下内容:

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

在实现加入这个(也增加了头文件):

- (void)keyboardWillShow:(NSNotification *)notification { 
#if __IPHONE_OS_VERSION_MAX_ALLOWED >= __IPHONE_3_2 
    if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone) { 
#endif 
#if __IPHONE_OS_VERSION_MIN_REQUIRED >= __IPHONE_3_2 
     NSValue *keyboardBoundsValue = [[notification userInfo] objectForKey:UIKeyboardFrameEndUserInfoKey]; 
#else 
     NSValue *keyboardBoundsValue = [[notification userInfo] objectForKey:UIKeyboardBoundsUserInfoKey]; 
#endif 
     CGRect keyboardBounds; 
     [keyboardBoundsValue getValue:&keyboardBounds]; 
     UIEdgeInsets e = UIEdgeInsetsMake(0, 0, keyboardBounds.size.height, 0); 
     [[self tableView] setScrollIndicatorInsets:e]; 
     [[self tableView] setContentInset:e]; 
#if __IPHONE_OS_VERSION_MAX_ALLOWED >= __IPHONE_3_2 
    } 
#endif 
} 

最后,在你的dealloc方法,添加:

[[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardWillShowNotification object:nil]; 

此代码来自ASIAuthenticati来自Ben Copsey的ASIHTTPRequest包装的onDialog类。希望能帮助到你。