以编程方式使用自动布局约束条件

问题描述:

我正尝试以编程方式在我的iOS应用程序中使用自动布局。 我有这个初始化代码以编程方式使用自动布局约束条件

UIView *innerView = [[UIView alloc] initWithFrame:self.view.bounds]; 

UIButton *button1 = [UIButton buttonWithType:UIButtonTypeRoundedRect]; 
[button1 setFrame:CGRectMake(50, 50, 150, 50)]; 
[button1 setTitle:@"button1" forState:UIControlStateNormal]; 

UIButton *button2 = [UIButton buttonWithType:UIButtonTypeRoundedRect]; 
[button2 setFrame:CGRectMake(250, 50, 150, 50)]; 
[button2 setTitle:@"button2" forState:UIControlStateNormal]; 

[innerView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"|[button1][button2(==button1)]|" options:NSLayoutFormatAlignAllBaseline metrics:0 views:NSDictionaryOfVariableBindings(button1, button2)]]; 

[innerView addSubview:button1]; 
[innerView addSubview:button2]; 
[self.view addSubview:innerView]; 

一个简单的视图控制器但是,当我试图运行它,我得到这个错误:

Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'Unable to parse constraint format: 
Unable to interpret '|' character, because the related view doesn't have a superview 
|[button1][button2(==button1)]| 
      ^' 

这有什么错呢?

,当我试图消除在constraintsWithVisualFormat管,我得到这样的警告:

Unable to simultaneously satisfy constraints. 
Probably at least one of the constraints in the following list is one you don't want. Try this: (1) look at each constraint and try to figure out which you don't expect; (2) find the code that added the unwanted constraint or constraints and fix it. (Note: If you're seeing NSAutoresizingMaskLayoutConstraints that you don't understand, refer to the documentation for the UIView property translatesAutoresizingMaskIntoConstraints) 
(
    "<NSAutoresizingMaskLayoutConstraint:0x716be10 h=--& v=--& UIRoundedRectButton:0x71631f0.midX == + 325>", 
    "<NSAutoresizingMaskLayoutConstraint:0x7169940 h=--& v=--& H:[UIRoundedRectButton:0x715fb80(150)]>", 
    "<NSAutoresizingMaskLayoutConstraint:0x7169860 h=--& v=--& UIRoundedRectButton:0x715fb80.midX == + 125>", 
    "<NSLayoutConstraint:0x7164cd0 UIRoundedRectButton:0x71631f0.width == UIRoundedRectButton:0x715fb80.width>", 
    "<NSLayoutConstraint:0x7164940 H:[UIRoundedRectButton:0x715fb80]-(0)-[UIRoundedRectButton:0x71631f0]>" 
) 

Will attempt to recover by breaking constraint 
<NSLayoutConstraint:0x7164940 H:[UIRoundedRectButton:0x715fb80]-(0)-[UIRoundedRectButton:0x71631f0]> 

Break on objc_exception_throw to catch this in the debugger. 
The methods in the UIConstraintBasedLayoutDebugging category on UIView listed in <UIKit/UIView.h> may also be helpful. 

而且由于这个结果,我的约束都完全没有影响。 我在做什么错?

你有三个问题:

首先,你需要选择加入到基于布局约束您既您的按钮子视图

[button1 setTranslatesAutoresizingMaskIntoConstraints:NO]; 
[button2 setTranslatesAutoresizingMaskIntoConstraints:NO]; 

这将摆脱NSAutoresizingMaskLayoutConstraint错误。现在您可以了解您的布局约束条件。

其次,您必须在添加约束之前addSubview。在button1和button2是innerview的子视图之前,您在innerView上调用addContraints

第三,您应该指定(尽管文档指出它是可选的)布局字符串是垂直还是水平。​​。您还需要像@"V:[button1]|"这样的垂直约束,它可以将button1对齐到innerview的底部,并且因为您已将按钮上的基线对齐,所以会出现button2。

+1

没错,'setTranslatesAutoresizingMaskIntoConstraints:NO'帮我摆脱这些错误的。但结果我得到button2与左上角对齐(button1不可见),但我仍然不能在'visualFormat'中使用管道。你有什么想法为什么错误说'相关的视图没有超视图。 UIViewController的'self.view'是我的'innerView'的超级视图,不是吗? “ – bulbform 2013-03-11 19:36:01

+2

”相关视图没有超级视图“是因为您添加了一个涉及尚未被插入到视图层次结构中的视图的约束。在所有调用addSubview:之后移动addConstraints:call。 – escrafford 2013-03-11 19:50:43

+0

谢谢,当我在'addSubview'语句中移动带有约束的代码块时,关于'无法解释管道'的错误消失了,并且我的布局与我想要的类似)谢谢,问题已解决) – bulbform 2013-03-11 19:54:38

只是为了记录:关于失踪的上海华异常被已被触发constraintsWithVisualFormat