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

我有一个静态单元的表视图.一个单元格包含一个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时没有注意到任何不同
>这是一个表视图控制器,并且自动调用ScrollViewInsets == 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.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.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 16 更新版本时,我们听到了困扰 Apple...
欧版/美版 特别说一下,美版选错了 可能会永久丧失4G,不过只...
一般在接外包的时候, 通常第三方需要安装你的app进行测...
前言为了让更多的人永远记住12月13日,各大厂都在这一天将应...