2014-07-22 47 views
0

我总是在代码中创建我的视图,因此从未使用过自动布局(直到现在)。不使用nib的原因是因为我更喜欢代码,通过GitHub等进行维护更简单,但足够了。Autolayout无法同时满足约束条件

我有一种情况,我在屏幕上放置了三个标签,彼此相邻。像这样:|Label1| - some space - |Label2| - some space - |Label3|。有些空间是让我们说15分。现在我想完成的是,当我更改标签的文本时,我会打电话给他们sizeToFit,并希望他们会保持彼此相距10pt。我知道我可以通过一些数学来解决这个问题,但我认为自动布局会更容易,更易于理解,并且我将在此学习一些新内容。因此,我所做的是:

  • 的Alloc &初始化所有的标签
  • 给每个标签的框架(和设置字体,文本颜色等)
  • 添加标签作为一个子视图

然后我试图在它们之间建立的约束,像这样:

NSArray *constraints = [NSLayoutConstraint constraintsWithVisualFormat:@"[label1]-15-[label2]-15-[label3]" options:0 metrics:nil views:NSDictionaryOfVariableBindings(label1, label2, label3)]; 
[self addConstraints:constraints]; //Im subclassing an UIView that's why self 

,但我得到一个错误说:

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) 
(
    "<NSLayoutConstraint:0x14c14110 H:[UILabel:0x14c12ab0]-(15)-[UILabel:0x14c12f80]>", 
    "<NSAutoresizingMaskLayoutConstraint:0x14c08b00 h=--& v=--& UILabel:0x14c12ab0.midX == + 195>", 
    "<NSAutoresizingMaskLayoutConstraint:0x14c0dd90 h=--& v=--& H:[UILabel:0x14c12ab0(40)]>", 
    "<NSAutoresizingMaskLayoutConstraint:0x14c1c170 h=--& v=--& UILabel:0x14c12f80.midX == + 237.5>", 
    "<NSAutoresizingMaskLayoutConstraint:0x14c12f00 h=--& v=--& H:[UILabel:0x14c12f80(40)]>" 
) 

Will attempt to recover by breaking constraint 
<NSLayoutConstraint:0x14c14110 H:[UILabel:0x14c12ab0]-(15)-[UILabel:0x14c12f80]> 

即使我试图在标签2和3上执行此操作,也会发生同样的情况。我不完全了解NSLog的说法。

任何帮助将不胜感激。此外,如果你有什么好的链接来解码那些h=--& v=--& UILabel:0x14c12f80.midX == + 237.5事情会很棒。

回答

2

h=v=是被转换为约束的自动修复掩码的签名。您是否忘记在您的某个视图上设置translatesAutoResizingMasksToConstraints = NO

相关问题