如何重置 UITextView 高度

How to reset UITextView height

我有一个聊天屏幕,UITextView 在 table 视图下方。我在 IB 中将 UITextView 高度设置为 <= 150。我让这个文本视图在我键入文本时调整大小和滚动。

在我发送消息并重新加载 table 视图后,文本视图的高度保持为 150,并且不会重置为原始大小。

重新加载后如何重置此高度table?

我在发送消息后尝试了以下解决方案,该消息会重置文本视图高度,但会在我再次输入文本视图时中断自动高度更改。

@IBOutlet weak var textViewHeightConstraint: NSLayoutConstraint!
//....
self.textViewHeightConstraint.constant = self.textView.contentSize.height

符合 UITextViewDelegate 并实施 textViewDidChange:

- (void)textViewDidChange:(UITextView *)textView
{
    CGFloat minHeight = 30;
    CGFloat maxHeight = 150;
    CGFloat height = ceil([textView.text sizeWithAttributes:@{NSFontAttributeName : textView.font}].height);
    if(height < minHeight){
        height = minHeight;
    }else if (height > maxHeight){
        height = maxHeight;
    }
    self.textViewHeightConstraint.constant = height;
}