ios autolayout 如何正确编写nslayoutconstriant?

Posted

技术标签:

【中文标题】ios autolayout 如何正确编写nslayoutconstriant?【英文标题】:ios autolayout how to write nslayoutconstriant correctly? 【发布时间】:2015-12-10 15:00:44 【问题描述】:

例如,我知道如何使用界面生成器来自动布局按钮。

但是当我尝试使用 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.

为什么?

【问题讨论】:

当您在代码中添加NSLayoutContraint 集时,您是否删除了情节提要中的那些,因此您不会尝试向同一个项目添加第二个集? @NikMos 我没有在界面生成器中添加任何约束。 终于得到***.com/questions/19015215/… 【参考方案1】:

在添加任何 NSLayoutConstraint 集之前,您应该记住将视图的 translatesAutoresizingMaskIntoConstraints 设置为 NOfalse。 我已经有一段时间没有与Objective-C 合作了,但如果我没记错的话,你可以这样写:

[self.thing1 setTranslatesAutoresizingMaskIntoConstraints: NO]

或在 Swift 2 中:

self.thing1.translatesAutoresizingMaskIntoConstraints = false

此外,您必须始终记住在添加任何约束之前将self.thing1 添加到self.view

【讨论】:

以上是关于ios autolayout 如何正确编写nslayoutconstriant?的主要内容,如果未能解决你的问题,请参考以下文章

如何使用大于或等于编程 iOS AutoLayout 约束而不使布局模糊?

UISearchBar AutoLayout ios7 错误?

iOS Autolayout - 在半透明导航栏下方正确定位视图

如何优雅的代码编写 AutoLayout

iOS进阶 - AutoLayout 是如何自动布局的,性能如何?

iOS进阶 - AutoLayout 是如何自动布局的,性能如何?