本文介绍了如何使用UITextview增加视图大小?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我做了一个示例演示,当我输入 UITextview
时,它会扩展,但视图不会扩展,这是 UITextview的超级视图
。
I have made one sample demo ,When I type on UITextview
it Will Expands but the view will not expand which is the super view of UITextview
.
- (void)textViewDidChange:(UITextView *)textView
{
CGFloat fixedWidth = textView.frame.size.width;
CGSize newSize = [textView sizeThatFits:CGSizeMake(fixedWidth, MAXFLOAT)];
CGRect newFrame = textView.frame;
newFrame.size = CGSizeMake(fmaxf(newSize.width, fixedWidth), newSize.height);
textView.frame = newFrame;
self.myTextView.scrollEnabled = NO;
CGRect viewFrame = self.myCommentView.frame;
viewFrame.size.height =self.myCommentView.frame.size.height + 1;
viewFrame.origin.y=self.myCommentView.frame.origin.y - 1;
self.myCommentView.frame =viewFrame;
[self.myTextView layoutIfNeeded];
[self.myCommentView layoutIfNeeded];
}
问题是
视图未按 UITextview
进行扩展。如果视图扩展 UITextview
未扩展。
Problem Is
View is not expanding with UITextview
.And If View Is Expanded UITextview
is not Expanding.
我希望两者都应该在 UITextview
展开时展开。
谢谢
推荐答案
声明 UITextViewDelegate
委托方法。
@property (strong, nonatomic) IBOutlet UITextView *txtview;
@property (strong, nonatomic) IBOutlet NSLayoutConstraint *backgroundviewhight; // myview hight constrain.
@property (strong, nonatomic) IBOutlet UIView *myview;
_txtview.delegate = self;
- (void)textViewDidChange:(UITextView *)textView
{
CGFloat fixedWidth = textView.frame.size.width;
CGSize newSize = [textView sizeThatFits:CGSizeMake(fixedWidth, MAXFLOAT)];
CGRect newFrame = textView.frame;
newFrame.size = CGSizeMake(fmaxf(newSize.width, fixedWidth), newSize.height);
textView.frame = newFrame;
self.txtview.scrollEnabled = NO;
_backgroundviewhight.constant = newFrame.size.height;
[self.txtview layoutIfNeeded];
[self.myview layoutIfNeeded];
}
您的textview约束
Your textview constrain like
创建约束你的myview hight约束的NSLayoutConstraint
create a constrain NSLayoutConstraint of your myview hight constrain
输出:
这篇关于如何使用UITextview增加视图大小?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!