【问题标题】:How to set autolayout constraint for UITextView如何为 UITextView 设置自动布局约束
【发布时间】:2014-03-24 05:03:01
【问题描述】:

我已经尝试了很多设置 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];

【问题讨论】:

    标签: ios uiview ios7 uitextview nslayoutconstraint


    【解决方案1】:

    是的,就像说@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}]];
    

    Verticale 和 Horizo​​ntale 告诉 text1 像 self.view frame

    【讨论】:

    • 非常感谢。 . . . .
    猜你喜欢
    • 1970-01-01
    • 2017-06-14
    • 1970-01-01
    • 1970-01-01
    • 2019-01-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-10-29
    相关资源
    最近更新 更多