说,在xib文件中我添加了一个UIView,它的宽度和高度是固定的。在xib文件中没有添加LayoutConstraint。我将UIView与IBOutlet属性“contentView”连接,然后在viewDidLoad方法中执行:
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view from its nib.
contentView.translatesAutoresizingMaskIntoConstraints = NO;
NSLayoutConstraint *constraint = [NSLayoutConstraint constraintWithItem:contentView attribute:NSLayoutAttributeRight relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeRight multiplier:1.0f constant:-20.0f];
[self.view addConstraint:constraint];
}
当我运行程序时,似乎发生了约束冲突,但我不知道如何解决这个问题:
2014-10-14 09:37:57.436 TestAutoLayout[651:53850] 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)
(
"<NSIBPrototypingLayoutConstraint:0x7a028de0 'IB auto generated at build time for view with fixed frame' H:|-(0)-[UIView:0x7a029380](LTR) (Names: '|':UIView:0x7a029470 )>",
"<NSIBPrototypingLayoutConstraint:0x7a028ed0 'IB auto generated at build time for view with fixed frame' H:[UIView:0x7a029380(320)]>",
"<NSLayoutConstraint:0x7a028620 UIView:0x7a029380.right == UIView:0x7a029470.right - 20>",
"<NSLayoutConstraint:0x7a02f600 'UIView-Encapsulated-Layout-Width' H:[UIView:0x7a029470(320)]>"
)
Will attempt to recover by breaking constraint
<NSLayoutConstraint:0x7a028620 UIView:0x7a029380.right == UIView:0x7a029470.right - 20>
出于某种原因,我必须在程序中设置约束。谢谢。