NSLayoutConstraint"无法同时满足约束条件"错误以及如何解释它们

时间:2015-02-23 11:07:51

标签: ios objective-c swift nslayoutconstraint

我遇到了一些限制问题,我无法理解。如果我更好地理解控制台窗口中的错误消息(特别是与NSLayoutConstraints相关),我想我会发现它真的很有帮助。这是一个例子:

我有这个约束:

NSLayoutConstraint(item: mainView, attribute: NSLayoutAttribute.Height, relatedBy: NSLayoutRelation.Equal, toItem: self.view, attribute: NSLayoutAttribute.Height, multiplier: 1, constant: 0)

这对我有意义,我想让mainViewself.view完全相同。但是这会产生错误:

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:0x7a94fa10 UIView:0x7a9f95c0.height == UIView:0x7a9d1660.height>",
    "<NSAutoresizingMaskLayoutConstraint:0x7a9488a0 h=--& v=--& V:[UIView:0x7a9f95c0(110)]>",
    "<NSLayoutConstraint:0x7a948490 'UIView-Encapsulated-Layout-Height' V:[UIView:0x7a9d1660(430)]>"
)

Will attempt to recover by breaking constraint 
<NSLayoutConstraint:0x7a94fa10 UIView:0x7a9f95c0.height == UIView:0x7a9d1660.height>

我已经注释掉除了这个之外的所有其他约束,但我仍然得到这个错误。我是否认为约束与自身相冲突?

有人能指出我如何解决这个问题吗?

最重要的是,如果有人可以帮助我理解这些非常有帮助的NSLayoutConstraint错误消息。

-

请注意我在这里使用Swift,但如果答案是在Objective C中,我不介意。

1 个答案:

答案 0 :(得分:4)

mainView.translatesAutoresizingMaskIntoConstraints = NO。 // ObjC mainView.translatesAutoresizingMaskIntoConstraints = false。 // Swift

它会起作用!

THX