在单元格中编辑UITextView时,iOS 7中的UITableView无法滚动到正确的位置

azs*_*mej 10 nslayoutmanager uitableview uitextview ios ios7

我有一个静态单元格的表视图.一个单元格包含a UITextView并且heightForRowAtIndexPath:是动态计算的,因此单元格总是足够高以容纳文本(该部分在iOS 7下进行了一些工作,实际上,因为它不再可能只是简单地询问textView contentSize).

当我在文本视图中点击开始编辑时,键盘动画到位,tableView上的contentInsets会自动调整以解决此问题(即,iPhone纵向方向的216px底部插入),光标/插入符号变为可见,然后表格视图滚动到另一个位置.它最终看起来像一个反弹.

以下是模拟器中的视频:https://www.dropbox.com/s/htdbb0t7985u6n4/textview-bounce.mov

请注意,插入符号位于键盘上方.我一直在记录表视图contentOffset,我可以看到它滚动到一个很好的值,然后突然"转身"并向后滚动.

奇怪的是,如果我在模拟器中打开慢动画,问题就会消失; 在contentOffset逆转不会发生的事情和工作,我希望(即iOS 6的行为).

以下是动画速度较慢的视频:https://www.dropbox.com/s/nhn7vspx86t4exb/textview-nobounce.mov

实施说明:

  • 文本视图为粉红色,并具有AutoLayout约束,使其固定在距离0处的单元格(左侧除外,即10pts)
  • 我正在使用boundingRectWithSize:计算表视图高度,调整lineFragmentPadding和任何顶部/底部插入.似乎工作.
  • 我已将textView设置为不可滚动,但在scrollEnabled== YES 时没有注意到任何不同
  • 这是一个表视图控制器,automaticallyAdjustsScrollViewInsets== YES

VCh*_*zov 3

尝试在键盘出现时调整 UITableView 框架。[self attachKeyboardHelper]在 viewWillAppear 和viewWillDisappear 中调用[self detachKeyboardHelper]

- (void)attachKeyboardHelper{
    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(keyboardWillAppear:)
                                                 name:UIKeyboardWillShowNotification
                                               object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(keyboardWillHide:)
                                                 name:UIKeyboardWillHideNotification
                                               object:nil];
}

- (void)detachKeyboardHelper{
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}

- (void)keyboardWillAppear:(NSNotification *)notification{
    NSDictionary* userInfo = [notification userInfo];

    NSTimeInterval animationDuration;
    UIViewAnimationCurve animationCurve;
    CGRect keyboardEndFrame;

    [[userInfo objectForKey:UIKeyboardAnimationCurveUserInfoKey] getValue:&animationCurve];
    [[userInfo objectForKey:UIKeyboardAnimationDurationUserInfoKey] getValue:&animationDuration];
    [[userInfo objectForKey:UIKeyboardFrameEndUserInfoKey] getValue:&keyboardEndFrame];

    // Animate up or down
    [UIView beginAnimations:nil context:nil];
    [UIView setAnimationDuration:animationDuration];

    CGRect keyboardFrame = [self.view convertRect:keyboardEndFrame toView:nil];
    if(self.view==self.tableView){
        CGRect newTableFrame = CGRectMake(self.tableView.frame.origin.x, self.tableView.frame.origin.y, self.tableView.frame.size.width, self.view.bounds.size.height-keyboardFrame.size.height);
        self.tableView.frame = newTableFrame;
    }else{
        CGRect newTableFrame = CGRectMake(self.tableView.frame.origin.x, self.tableView.frame.origin.y, self.tableView.frame.size.width, self.view.bounds.size.height-self.tableView.frame.origin.y-keyboardFrame.size.height);
        self.tableView.frame = newTableFrame;
    }

    [UIView commitAnimations];
}

- (void)keyboardWillHide:(NSNotification *)notification{
    NSDictionary* userInfo = [notification userInfo];

    NSTimeInterval animationDuration;
    UIViewAnimationCurve animationCurve;
    CGRect keyboardEndFrame;

    [[userInfo objectForKey:UIKeyboardAnimationCurveUserInfoKey] getValue:&animationCurve];
    [[userInfo objectForKey:UIKeyboardAnimationDurationUserInfoKey] getValue:&animationDuration];
    [[userInfo objectForKey:UIKeyboardFrameEndUserInfoKey] getValue:&keyboardEndFrame];


    CGRect newTableFrame = CGRectMake(self.tableView.frame.origin.x, self.tableView.frame.origin.y, self.tableView.frame.size.width, self.view.superview.bounds.size.height-self.tableView.frame.origin.y);
    self.tableView.frame = newTableFrame;
    if(newTableFrame.size.height>self.tableView.contentSize.height-self.tableView.contentOffset.y){
        float newOffset=MAX(self.tableView.contentSize.height-newTableFrame.size.height, 0);
        [self.tableView setContentOffset:CGPointMake(0, newOffset) animated:YES];
    }

}
Run Code Online (Sandbox Code Playgroud)