在单元格中编辑UITextView时,iOS 7中的UITableView无法滚动到正确的位置 [英] UITableView in iOS 7 not scrolling to correct location when editing UITextView in cell

查看:116
本文介绍了在单元格中编辑UITextView时,iOS 7中的UITableView无法滚动到正确的位置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个带有静态单元格的表视图。一个单元格包含 UITextView ,并且动态计算 heightForRowAtIndexPath:,以便单元格总是高到足以容纳文本(那部分在iOS 7下做了一些工作,实际上,因为它不再可能只是简单地要求textView查找其 contentSize )。

I have a table view with static cells. One cell contains a UITextView and the heightForRowAtIndexPath: is calculated dynamically so that the cell is always tall enough to accomodate the text (that part took some work under iOS 7, actually, as it's no longer possible to simply ask the textView for its contentSize).

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

When I tap within the text view to start editing, the keyboard animates into place, the contentInsets on the tableView are automatically adjusted to account for this (ie, bottom inset of 216px for iPhone portrait orientation), the cursor / caret becomes visible, and then the table view scrolls to another location. It ends up looking like a bounce.

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

Here's a video of this in the simulator: https://www.dropbox.com/s/htdbb0t7985u6n4/textview-bounce.mov

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

Notice that for a second the caret is just above the keyboard. I've been logging the table view's contentOffset and I can see it scroll to a nice value and then suddenly "turn around" and scroll back.

奇怪的是,如果我在模拟器中打开慢动画,问题就会消失; contentOffset 反转不会发生,事情会按照我的预期发挥作用(即iOS 6行为)。

Oddly, if I turn on slow animations in the simulator the problem disappears; the contentOffset reversal doesn't happen and things work as I expect (ie, iOS 6 behavior).

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

Here's the video with slow animations: https://www.dropbox.com/s/nhn7vspx86t4exb/textview-nobounce.mov

实施说明:


  • 文字视图为粉红色并且具有AutoLayout约束,使其在距离0处固定到单元格(左侧除外,即10pts)

  • 我正在使用 boundingRectWithSize:计算表视图高度,调整lineFragmentPadding和任何顶部/底部插入。似乎工作。

  • 我已将textView设置为不可滚动,但在 scrollEnabled == YES时没有注意到任何不同之处

  • 这是一个表视图控制器, automaticAdjustsScrollViewInsets == YES

  • The text view is pink and has AutoLayout constraints that keep it pinned to the cell at distance 0 (except left side, which is 10pts)
  • I'm using boundingRectWithSize: to calculate the table view height, adjusting for lineFragmentPadding and any top/bottom insets. Seems to work.
  • I have set the textView to not be scrollable, but didn't notice anything different when scrollEnabled == YES
  • This is a table view controller and automaticallyAdjustsScrollViewInsets == YES

推荐答案

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

Try to adjust UITableView frame when keyboard appears. Call [self attachKeyboardHelper] in viewWillAppear and [self detachKeyboardHelper] in viewWillDisappear.

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

}

这篇关于在单元格中编辑UITextView时,iOS 7中的UITableView无法滚动到正确的位置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆