gpt4 book ai didi

ios - 如何使用 UITextview 增加 View 大小?

转载 作者:行者123 更新时间:2023-11-29 00:31:25 25 4
gpt4 key购买 nike

我做了一个示例演示,当我在 UITextview 上键入时,它会展开,但 View 不会展开,这是 UITextview 的 super View 。

代码

- (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];
}

问题是

View 未随 UITextview 展开。如果 View 展开,UITextview 也不会展开。

图片

enter image description here

UITextView 约束

enter image description here

Storyboard

enter image description here我希望当 UITextview 展开时两者都应该展开。

enter image description here谢谢你

最佳答案

声明 UITextViewDelegate 委托(delegate)方法。

@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 constrain like

Your textview constrain

为您的 myview 高度约束创建一个 NSLayoutConstraint 约束

Myview constrain

输出:

Here

关于ios - 如何使用 UITextview 增加 View 大小?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41675812/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com