我正在学习自动布局而不使用StoryBoard。我给出了尾随,领先,顶部和底部的四个约束。
代码:
UITextField *searchBarTF=[[UITextField alloc]init];
[searchBarTF setText:@"is it coming?"];
[views addSubview:searchBarTF];
[views addConstraint:[NSLayoutConstraint constraintWithItem:searchBarTF attribute:NSLayoutAttributeLeading relatedBy:NSLayoutRelationEqual toItem:views attribute:NSLayoutAttributeLeading multiplier:1.0 constant:30]];
[views addConstraint:[NSLayoutConstraint constraintWithItem:searchBarTF attribute:NSLayoutAttributeTrailing relatedBy:NSLayoutRelationEqual toItem:views attribute:NSLayoutAttributeTrailing multiplier:1.0 constant:30]];
[views addConstraint:[NSLayoutConstraint constraintWithItem:searchBarTF attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:views attribute:NSLayoutAttributeTop multiplier:1.0 constant:30]];
[views addConstraint:[NSLayoutConstraint constraintWithItem:searchBarTF attribute:NSLayoutAttributeBottom relatedBy:NSLayoutRelationEqual toItem:views attribute:NSLayoutAttributeBottom multiplier:1.0 constant:230]];
TextField未出现在屏幕上。
错误:
Unable to simultaneously satisfy constraints.
Probably at least one of the constraints in the following list is one you don't want.
答案 0 :(得分:3)
你忘了这段代码:。
[searchBarTF setTranslatesAutoresizingMaskIntoConstraints:NO];
默认情况下,这是True
。因此,您的约束与自动调整约束相冲突。
以上行将禁用文本字段的自动调整大小,它将应用您定义的约束。