ios - 在单元格中编辑 UITextView 时,iOS 7 中的 UITableView 不会滚动到正确的位置

标签 ios uitableview ios7 uitextview nslayoutmanager

我有一个带有静态单元格的表格 View 。一个单元格包含一个 UITextView 并且 heightForRowAtIndexPath: 是动态计算的,因此单元格总是足够高以容纳文本(这部分在 iOS 7 下需要一些工作,实际上,因为不再可能简单地向 textView 询问其 contentSize)。

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

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

请注意,有那么一瞬间,插入符号就在键盘上方。我一直在记录 TableView 的 contentOffset,我可以看到它滚动到一个合适的值,然后突然“转身”并向后滚动。

奇怪的是,如果我在模拟器中打开慢速动画,问题就会消失; contentOffset 反转没有发生,一切都按我预期的那样工作(即 iOS 6 行为)。

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

实现说明:

  • TextView 是粉红色的,并且具有自动布局约束,使其固定到距离为 0 的单元格(左侧除外,为 10pts)
  • 我正在使用 boundingRectWithSize: 计算表格 View 高度,调整 lineFragmentPadding 和任何顶部/底部插图。似乎有效。
  • 我已将 textView 设置为不可滚动,但在 scrollEnabled == YES
  • 时没有注意到任何不同
  • 这是一个 TableView Controller 和automaticallyAdjustsScrollViewInsets == YES

最佳答案

尝试在出现键盘时调整 UITableView 框架。在 viewWillAppear 中调用 [self attachKeyboardHelper] 并在 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];
    }

}

关于ios - 在单元格中编辑 UITextView 时,iOS 7 中的 UITableView 不会滚动到正确的位置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19192099/

相关文章:

iOS:OpenCV 红色范围

ios - 重新加载 searchResultsTableView 时自定义单元格错误

ios : Nullpointer exception

ios - 将 “correct” 数据从 UITableViewController 中的选定单元格传递到 ViewController

ios - 第二次添加/删除断言失败

ios - 滚动表格 View 以在文本字段中输入文本?

iphone - 将 UIAppearance 与 UIBarButtonItem 的子类一起使用会导致无法识别的选择器被发送到 UINavigationButton

ios - AutoLayout 的 systemLayoutSizeFittingSize 仅适用于 UILabels?

ios - Xcode5 : The app references non-public selectors in Payload/<AppName>. 应用/<AppName>: setAttribution:

iphone - 更改 Safari 和 Chrome 中 iOS 7 状态栏的颜色