但是当我尝试使用 nslayoutconstraint 时,
[self.view addConstraint:[NSLayoutConstraint constraintWithItem:self.thing1 attribute:NSLayoutAttributeLeading relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeLeadingMargin multiplier:1 constant:0]];
[self.view addConstraint:[NSLayoutConstraint constraintWithItem:self.thing1 attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:[self topLayoutGuide] attribute:NSLayoutAttributeBottom multiplier:1 constant:0]];
}
我认为它应该与界面生成器中的约束相同。
但我收到了以下错误消息:
2015-12-10 22:59:01.320 Storyboard[7647:5116727] 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.
(
"<NSLayoutConstraint:0x12fd39a60 UIButton:0x12fd3c4f0'thing1'.leading == UIView:0x12fd3d590.leadingMargin>",
"<NSIBPrototypingLayoutConstraint:0x12fe48040 'IB auto generated at build time for view with fixed frame' H:|-(20)-[UIButton:0x12fd3c4f0'thing1'](LTR) (Names: '|':UIView:0x12fd3d590 )>"
)
Will attempt to recover by breaking constraint
<NSLayoutConstraint:0x12fd39a60 UIButton:0x12fd3c4f0'thing1'.leading == UIView:0x12fd3d590.leadingMargin>
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.
2015-12-10 22:59:01.327 Storyboard[7647:5116727] 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.
(
"<_UILayoutSupportConstraint:0x12fea5ac0 V:[_UILayoutGuide:0x12fd3d960(0)]>",
"<_UILayoutSupportConstraint:0x12fe9e960 V:|-(0)-[_UILayoutGuide:0x12fd3d960] (Names: '|':UIView:0x12fd3d590 )>",
"<NSLayoutConstraint:0x12fd3d800 V:[_UILayoutGuide:0x12fd3d960]-(0)-[UIButton:0x12fd3c4f0'thing1']>",
"<NSIBPrototypingLayoutConstraint:0x12fea5e40 'IB auto generated at build time for view with fixed frame' V:|-(20)-[UIButton:0x12fd3c4f0'thing1'] (Names: '|':UIView:0x12fd3d590 )>"
)
Will attempt to recover by breaking constraint
<NSLayoutConstraint:0x12fd3d800 V:[_UILayoutGuide:0x12fd3d960]-(0)-[UIButton:0x12fd3c4f0'thing1']>
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.
为什么?
最佳答案
在添加任何一组 NSLayoutConstraint
之前,您应该记住将 View 的 translatesAutoresizingMaskIntoConstraints 设置为 NO
或 false
。
我已经有一段时间没有使用 Objective-C
了,但如果我没记错的话,你可以这样写:
[self.thing1 setTranslatesAutoresizingMaskIntoConstraints: NO]
或者在 Swift 2 中:
self.thing1.translatesAutoresizingMaskIntoConstraints = false
此外,您必须始终记住在添加任何约束之前将 self.thing1
添加到 self.view
。
关于ios自动布局如何正确编写nslayoutconstraint?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34205158/