隐藏键盘后UITableView不会滚动

时间:2011-09-22 02:19:37

标签: iphone objective-c ios

我有一个带自定义单元格的UITableView。基于对此question的回答,我正在调整视图的大小,以便在键盘关闭时调整键盘并调整大小。取消键盘后,表格视图不再滚动。

这些是显示和隐藏键盘时调用的方法:

-(void)keyboardWillShow:(NSNotification *)note
 {
    NSDictionary* userInfo = [note userInfo];

    NSValue* keyboardFrameValue = [userInfo objectForKey:@"UIKeyboardBoundsUserInfoKey"];
    if (!keyboardFrameValue) {
            keyboardFrameValue = [userInfo objectForKey:@"UIKeyboardFrameEndUserInfoKey"];
    }

    // Reduce the tableView height by the part of the keyboard that actually covers the tableView
    CGRect windowRect = [[UIApplication sharedApplication] keyWindow].bounds;
    CGRect viewRectAbsolute = [myTableView convertRect:myTableView.bounds toView:[[UIApplication sharedApplication] keyWindow]];

    CGRect frame = myTableView.frame;
    frame.size.height -= [keyboardFrameValue CGRectValue].size.height - CGRectGetMaxY(windowRect) + CGRectGetMaxY(viewRectAbsolute);

    [UIView beginAnimations:nil context:NULL];
    [UIView setAnimationDuration:[[userInfo objectForKey:UIKeyboardAnimationDurationUserInfoKey] doubleValue]];
    [UIView setAnimationCurve:[[userInfo objectForKey:UIKeyboardAnimationCurveUserInfoKey] intValue]];
    myTableView.frame = frame;
    [UIView commitAnimations];

    UITableViewCell *textFieldCell = (id)((UITextField *)self.textFieldBeingEdited).superview.superview;
    NSIndexPath *textFieldIndexPath = [myTableView indexPathForCell:textFieldCell];

    [NSObject cancelPreviousPerformRequestsWithTarget:self];

    [myTableView scrollToRowAtIndexPath:textFieldIndexPath atScrollPosition:UITableViewScrollPositionMiddle animated:YES];
}

-(void)keyboardWillHide:(NSNotification *)note
{
    CGRect keyboardRect = [[[note userInfo] objectForKey:UIKeyboardFrameEndUserInfoKey] CGRectValue];
    NSTimeInterval animationDuration = [[[note userInfo] objectForKey:UIKeyboardAnimationDurationUserInfoKey] doubleValue];
    CGRect frame = self.myTableView.frame;
    frame.size.height += keyboardRect.size.height + 49;
    [UIView beginAnimations:@"ResizeForKeyboard" context:nil];
    [UIView setAnimationDuration:animationDuration];
    self.myTableView.frame = frame;
    [UIView commitAnimations];
    myTableView.scrollEnabled = YES;
}

我缺少什么想法?

2 个答案:

答案 0 :(得分:0)

我使用了您链接到的相同问题来解决同样的问题。虽然我不记得我最终使用了多少原始代码,但它一直对我很有用。

对于你的问题,(虽然我想你已经尝试过这个),首先想到的是查看你的代码,看看你是否正在做

self.tableView.scrollEnabled = NO;

如果是这样,你应该验证你有一个相应的声明在某处将其设置回YES;事实上,您可以在keyboardWillHide中将scrollEnabled设置为YES,以测试是否有帮助。

答案 1 :(得分:0)

有问题的一行是:

frame.size.height += keyboardRect.size.height + 49;

它应该是:

frame.size.height += keyboardRect.size.height - self.navigationController.toolbar.frame.size.height;