为什么为所有四个边添加约束会导致错误?

时间:2015-08-06 09:44:03

标签: ios cocoa-touch autolayout

更新

enter image description here编辑一个XIB文件,我在视图中添加了一个按钮到我想要的位置,然后单击右下角的图钉图标,并用“约束”固定按钮的所有四个边。到边缘“未经检查,因为这是我想要放置按钮的确切位置,我不希望它移动。

但我收到的错误是“无法同时满足约束条件”。为什么会这样?

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) 
(
"<_UILayoutSupportConstraint:0x9f494b0 V:[_UILayoutGuide:0x9f40370(20)]>",
"<_UILayoutSupportConstraint:0x9f3b6c0 V:|-(0)-[_UILayoutGuide:0x9f40370]   (Names: '|':UIView:0x9f3feb0 )>",
"<_UILayoutSupportConstraint:0x9f418e0 V:[_UILayoutGuide:0x9f407c0(0)]>",
"<_UILayoutSupportConstraint:0x9f18010 _UILayoutGuide:0x9f407c0.bottom == UIView:0x9f3feb0.bottom>",
"<NSLayoutConstraint:0x9f40f10 V:[UIButton:0x9f40f40'Button']-(211)-[_UILayoutGuide:0x9f407c0]>",
"<NSLayoutConstraint:0x9f47ab0 V:[_UILayoutGuide:0x9f40370]-(539)-[UIButton:0x9f40f40'Button']>",
"<NSLayoutConstraint:0xa157b10 'UIView-Encapsulated-Layout-Height' V:[UIView:0x9f3feb0(568)]>"
)

Will attempt to recover by breaking constraint 

Make a symbolic breakpoint at UIViewAlertForUnsatisfiableConstraints to catch this in the debugger.
The methods in the UIConstraintBasedLayoutDebugging category on UIView listed in <UIKit/UIView.h> may also be helpful.

2 个答案:

答案 0 :(得分:0)

Autolayout技术处理对象与对象之间的关系,以及超视图如何在屏幕上绘制。正如你所提到的,你试图通过所有方面强制对象,所以设置特定位置你可以尝试通过应用特定约束来思考对象将如何在屏幕上绘制。

如果您想将固定宽度和高度的对象放置在所有方向的屏幕中心,那么您可以尝试

对齐 - &gt;容器中的水平和垂直中心。 (否则,如果不满足约束,他们甚至会崩溃你的应用程序)

希望这会对你有所帮助。

答案 1 :(得分:0)

如果您为按钮指定了高度或宽度,它将与您添加的约束冲突。如果您希望它始终处于特定的x,y和特定尺寸,则应将顶部和左侧与宽度和高度一起固定。如果你想要它动态调整大小,你应该固定所有4个边。