gpt4 book ai didi

ios - 如何为 UITextView 设置自动布局约束

转载 作者:行者123 更新时间:2023-12-01 19:02:29 25 4
gpt4 key购买 nike

我已经尝试了很多来为 UITextView 设置自动布局约束。但是当我运行时,我得到空白屏幕。我在 UIButton 中尝试了相同的方法,效果很好!!!!!!!

是否有任何属性要设置为 UITextView 分配约束????

UITextView *text1;
text1=[[UITextView alloc]initWithFrame:CGRectMake(50, 100, 100, 30)];
[text1 setBackgroundColor:[UIColor blueColor]];
set.text1=@"asjdgajsdbasbfbsdfbsdbfbsd";

[text1 sizeToFit];
set.translatesAutoresizingMaskIntoConstraints=NO;
[self.view addSubview:text1];


NSLayoutConstraint *constraint;
constraint = [NSLayoutConstraint constraintWithItem:text1 attribute:NSLayoutAttributeBottom relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeBottom multiplier:1.0f constant:1.f];

[self.view addConstraint:constraint];

最佳答案

是的,就像说@P.Sami 你需要更多的约束,因为线
set.translatesAutoresizingMaskIntoConstraints=NO;
禁用 autoresizeMask 这意味着您的框架无法正常工作。并且所有帧都由约束来计算。
在您的事件中,只有一个约束描述了这一点:constraint = [NSLayoutConstraint constraintWithItem:text1 attribute:NSLayoutAttributeBottom relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeBottom multiplier:1.0f constant:1.f];这个约束告诉:“text1.bottom like self.view.bottom * 1 + 1”首先它不等于底部边缘text1和superview,而是向下偏移1点。其次,我们不知道高度、体重、x、y 怎么样?
对于简单的示例,只需添加附加约束:

[self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|[text]|" options:0 metrics:nil views:@{text1}]];
[self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|[text]|" options:0 metrics:nil views:@{text1}]];

它被告知 text1 就像 Verticale 和 Horizo​​ntale 的 self.view 框架

关于ios - 如何为 UITextView 设置自动布局约束,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21955485/

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