ios - UITextView 光标/滚动移动问题

标签 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];
    }
}

有什么解决办法吗?

最佳答案

我通过编写 [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];
    }
}

关于ios - UITextView 光标/滚动移动问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33030110/

相关文章:

ios - 如何更改 UISearchBar 的背景颜色和占位符位置

ios - 对象加载 UIImage 使用 swift 使程序执行崩溃

ios - 如何设置带有角半径、边框和阴影的 UIView mask ?

ios - Objective-C(IOS 6.1+): Draw Text in a shape or path

ios - 键入时 TextView 中的 AttributedText

ios - MK FabButton 中的图标颜色

objective-c - 在ios中打开视频到时间偏移

objective-c - 用于 iphone 应用程序的自定义黑色键盘

ios - 带有 UIButton 的 UILabel

ios - 为什么我的 UITextView 中的文本在我按下回车键之前是透明的?