根据内容调整textview和xib框架高度

时间:2016-06-09 06:02:36

标签: ios objective-c textview

这是XIB文件( ChatInputBar.xib

enter image description here

我正在根据内容并使用此代码尝试更新textview的高度

- (void)textViewDidChange:(UITextView *)textView{
    CGRect frame = textView.frame;
    frame.size.height = textView.contentSize.height;

    if(frame.size.height > textView.frame.size.height){
        CGFloat diff = frame.size.height - textView.frame.size.height;
        textView.frame = CGRectMake(textView.frame.origin.x, textView.frame.origin.y - diff, textView.frame.size.width, frame.size.height);
    }
    else if(frame.size.height < textView.frame.size.height){
        CGFloat diff = textView.frame.size.height - frame.size.height;
        textView.frame = CGRectMake(textView.frame.origin.x, textView.frame.origin.y + diff, textView.frame.size.width, frame.size.height);
    }
}

我得到了这个结果

enter image description here

我怎样才能更新整个视图的高度?

0 个答案:

没有答案