UITextView光标/滚动移动问题

时间:2015-10-09 04:49:22

标签: ios objective-c uitextview

我使用UITextView进行聊天,就像这里附带的截图一样。 enter image description here

但问题是如果我按下键盘的返回键,光标不会粘在UITextView边界的底部。我也使用了以下代码:

- (void)textViewDidChange:(UITextView *)textView
{
    if([textView contentSize].height <80.0)
    {
        CGFloat textHeight         = [self textHeightForTextView:textView];

        CGFloat newViewHeight = MAX(MIN(textHeight, 80.0), 33.0);

        chatTxtHeightConstraint.constant = newViewHeight;

        [textView scrollRangeToVisible:textView.selectedRange];
    }
}

任何解决方案?

1 个答案:

答案 0 :(得分:1)

我通过编写[textView layoutIfNeeded];[textView updateConstraints];以下代码行chatTxtHeightConstraint.constant = newViewHeight;来解决问题

- (void)textViewDidChange:(UITextView *)textView
{
    if([textView contentSize].height <80.0)
    {
        CGFloat textHeight         = [self textHeightForTextView:textView];

        CGFloat newViewHeight = MAX(MIN(textHeight, 80.0), 33.0);

        chatTxtHeightConstraint.constant = newViewHeight;

        [textView layoutIfNeeded];
        [textView updateConstraints];

        [textView scrollRangeToVisible:textView.selectedRange];
    }
}