使用UITableView的动态UITextView中的光标不再适用于iOS 11

Mar*_*eid 12 uitableview uitextview ios autolayout swift

我在一个人类型中创建了一个UITextView动态调整大小UITableViewCell.

在iOS 10上,UITextView自动跟踪光标,只需要很少的代码.

UITextViewDelegate方法中textViewDidChange:我使用了这个代码,它更新了文本视图的大小,没有任何跳转.

func textViewDidChange(_ textView: UITextView) {
    UIView.setAnimationsEnabled(false)
    self.tableView.beginUpdates()
    self.tableView.endUpdates()
    UIView.setAnimationsEnabled(true)
    self.tableView.contentOffset = currentOffset
}
Run Code Online (Sandbox Code Playgroud)

在iOS 11中,这不再有效,当我键入时,光标在键盘下消失.请注意,键盘出现时我不需要任何更改.

我知道iOS 11中的内容与内容插件的工作方式有关,但无法弄清楚我需要做些什么改变才能使其工作.

我应该在哪里进行这些修改来修复它?

- 更新 -

事实证明,删除所有代码解决了我在iOS 11和iOS 11中的问题处理向下滚动以跟随光标,因为我自动键入没有任何问题.

我剩下的一个问题是,在停止更新UITextViews大小之前,我只能在UITextView中输入多达28行文本.

rod*_*her 7

我有同样的问题,UITableViewController与UITableViewCell在单元格中包含一个非滚动的可编辑UITextView,光标将在iOS11的键盘后面.以前的iOS版本工作得很好.

我终于想出了一个基于这个和其他文章的修复:

- (void)textViewDidChange:(UITextView *)textView {
    // handle to UITableView
    UITableView *tableView = ....

    // tell table to resize itself
    [UIView performWithoutAnimation:^{
        [tableView beginUpdates];
        [tableView endUpdates];
    }];

    // get the caret rectangle so we can make sure we scroll it into view.
    CGRect unconvertedRect = [textView caretRectForPosition:textView.selectedTextRange.start];
    CGRect caretRect = [textView convertRect:unconvertedRect toView:tableView];
    // make the rect a little bigger so it's not at the extreme bottom of the view area
    caretRect.size.height += caretRect.size.height / 2;

    // this doesn't seem to work with a simple dispatch_async, but dispatch_after 0.1s seems to work.
    // why, i have no idea.
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        [tableView scrollRectToVisible:caretRect animated:NO];
    });
}
Run Code Online (Sandbox Code Playgroud)

希望这有助于某人......