当我做了UIView
的子视图的初始设置,如果我初始化一个UIView
:AutoLayout代码应放在哪里?
//Listing A
UIView *redBox = [[UIView alloc] init];
[redBox setBackgroundColor:[UIColor redColor]];
[self.view addSubview:redBox];
[redBox setTranslatesAutoresizingMaskIntoConstraints:NO];
而且我想以编程方式应用自动布局约束它:
//Listing B
NSArray *constraints = [NSLayoutConstraint constraintsWithVisualFormat:@"H:|-[redBox(100)]" options:0 metrics:nil views:@{@"redBox": redBox}];
[self.view addConstraints:constraints];
constraints = [NSLayoutConstraint constraintsWithVisualFormat:@"V:|-[redBox(100)]" options:0 metrics:nil views:@{@"redBox": redBox}];
[self.view addConstraints:constraints];
在哪里应列出B去?
它应该总是紧随清单A?
或者,如果我们在一个UIView
子类,它应该进入:
updateConstraints
initWithFrame
或layoutSubviews
?
同样,如果我们在一个UIViewController
子类,我把列表A中viewDidLoad
,应列表B紧跟它还是应该进入:
initWithNibName:bundle:
viewWillLayoutSubviews
或updateViewConstraints
?
错了。如果您在xib中定义的约束与您在'viewDidLoad'中定义的约束相抵触,您将会看到“无法同时满足约束”错误。 – Eric
同样,你是在问一个问题,还是在测试他人的知识?你似乎比那些试图帮助你的人更了解。 – Fogmeister
无法同时满足约束错误?我真的不知道这个术语,如果你知道,那么请更好地解释,然后指出某人回答消极! – Retro