我使用UITextView进行聊天,就像这里附带的截图一样。
但问题是如果我按下键盘的返回键,光标不会粘在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];
}
}
任何解决方案?
答案 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];
}
}