开发者

UITableView won't scroll after keyboard is hidden

I have a UITableView with custom cells. 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:UIKeyboardAnimationDuration开发者_运维知识库UserInfoKey] 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; in fact you might set scrollEnabled to YES in keyboardWillHide just to test if that helps.


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;
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜