简体   繁体   English

隐藏键盘后,UITableView将不会滚动

[英]UITableView won't scroll after keyboard is hidden

I have a UITableView with custom cells. 我有一个带有自定义单元格的UITableView。 Based on an answer to this question I am resizing the view when to accommodate the keyboard and resizing when the keyboard is dismissed. 基于对这个问题的回答,我在调整视图的大小以适应键盘,并在关闭键盘时调整视图。 After dismissing the keyboard the table view no longer scrolls. 消除键盘后,表格视图不再滚动。

These are the methods called when showing and hiding the keyboard: 这些是显示和隐藏键盘时调用的方法:

-(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;
}

Any ideas what I am missing? 有什么想法我想念的吗?

I've used the same question you link to to solve the same problem. 我使用了您链接的同一问题来解决同一问题。 It's been working great for me although I don't remember how much of the original code I ended up using. 尽管我不记得我最终使用了多少原始代码,但对我来说一直很好。

For your problem, (though I imagine you've tried this) the first thing that comes to mind is to look in your code to see if your doing 对于您的问题,(尽管我想您已经尝试过了),首先想到的是查看您的代码,看看您是否在做

self.tableView.scrollEnabled = NO;

and if so you should verify that you have a corresponding statement somewhere sets it back to YES; 如果是这样,则应验证某处是否有相应的语句将其设置为YES; in fact you might set scrollEnabled to YES in keyboardWillHide just to test if that helps. 实际上,您可以在keyboardWillHide中将scrollEnabled设置为YES,以测试是否有帮助。

The problematic line is: 有问题的行是:

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

it should be: 它应该是:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM