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

问题描述:

我遇到了一些问题,我无法得到我的头。如果我更好地理解控制台窗口中的错误消息(具体涉及到NSLayoutConstraints),我想我会发现它非常有用。这里有一个例子:NSLayoutConstraint“无法同时满足约束条件”错误以及如何解释它们

我有这样的约束:

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

这对我来说很有意义,我想使mainView完全一样的高度self.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错误消息,那将是非常有用的。

-

请注意我用的雨燕在这里,但我不介意,如果答案是C.目的

+0

is mainView is Dynamic/Static in Interface Builder ..? 如果它是静态的,那么你可以同时选择视图和主视图,并保持高度等于它的限制。 – Vidhyanand 2015-02-23 11:11:40

+0

我正在以编程方式构建我的所有UIView – Jimmery 2015-02-23 11:13:23

+1

将mainView.translatesAutoresizingMaskIntoConstraints设置为NO。 – Mornirch 2015-02-23 11:13:57

mainView.translatesAutoresizingMaskIntoConstraints = NO。 // ObjC mainView.translatesAutoresizingMaskIntoConstraints = false。 //斯威夫特

它会工作!

thx

+0

但使用false而不是NO。 – 2015-02-23 11:23:26

+0

我忘了....,@Dániel纳吉 – Mornirch 2015-02-23 11:26:43