ios自动布局如何正确编写nslayoutconstraint?

标签 ios autolayout

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

但是当我尝试使用 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 设置为 NOfalse
我已经有一段时间没有使用 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/

相关文章:

ios - 核心数据功能的最佳实践

c# - 尝试使用 PCLStorage 读取文件时卡住

iphone - 关于 AudioQueue : request to trim 0 + 1676 = 1676 frames from buffer containing 1152 frames 的 XCODE AVAudioPlayer 错误

ios - 使用 UIImagePickerController 加载图像的纵横比

ios - 为什么有这么多canOpenURL?

ios - 父 View 框架尺寸更改后更新约束

ios - 断言失败 - 具有自动布局和自定义 UICollectionViewFlowLayout 的 UITableViewCell 内的 CollectionView

iphone - 在 View Controller 的宽度上水平均匀分布 UIButton 的最简单方法?

ios - 使用 NSLayoutConstraints 均匀间隔多行任意数量的元素

ios - 禁用自动布局本地化行为(RTL - 从右到左行为)