有了PANKAJ VERMA 的answer,它终于有意义了。
就我而言,我有一个 ImageView 并且只设置了 2 个约束,但错误表明我有更多约束并且 LayoutConstraints 无法同时满足约束 :
view.addSubview(imageView)
let yConstraint = imageView.centerYAnchor.constraint(equalTo: layout.centerYAnchor)
yConstraint.identifier = "yConstraint"
let xConstraint = imageView.centerXAnchor.constraint(equalTo: layout.centerXAnchor)
xConstraint.identifier = "xConstraint"
错误信息:
[LayoutConstraints] 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)
(
"<NSAutoresizingMaskLayoutConstraint:0x600000089360 h=--& v=--& UIImageView:0x7fc1782087b0.minY == 0 (active, names: '|':UIView:0x7fc176406220 )>",
"<NSAutoresizingMaskLayoutConstraint:0x600000089220 h=--& v=--& UIImageView:0x7fc1782087b0.height == 0 (active)>",
"<NSLayoutConstraint:0x600000088c30 'UIView-bottomMargin-guide-constraint' V:[UILayoutGuide:0x600001a880e0'UIViewLayoutMarginsGuide']-(34)-| (active, names: '|':UIView:0x7fc176406220 )>",
"<NSLayoutConstraint:0x600000088cd0 'UIView-topMargin-guide-constraint' V:|-(48)-[UILayoutGuide:0x600001a880e0'UIViewLayoutMarginsGuide'] (active, names: '|':UIView:0x7fc176406220 )>",
"<NSLayoutConstraint:0x600000088d70 'yConstraint' UIImageView:0x7fc1782087b0.centerY == UILayoutGuide:0x600001a880e0'UIViewLayoutMarginsGuide'.centerY (active)>"
)
Will attempt to recover by breaking constraint
<NSLayoutConstraint:0x600000088d70 'yConstraint' UIImageView:0x7fc1782087b0.centerY == UILayoutGuide:0x600001a880e0'UIViewLayoutMarginsGuide'.centerY (active)>
Make a symbolic breakpoint at UIViewAlertForUnsatisfiableConstraints to catch this in the debugger.
The methods in the UIConstraintBasedLayoutDebugging category on UIView listed in <UIKitCore/UIView.h> may also be helpful.
我设置的第二个约束 ("xConstraint") 基本上有一个几乎相同的错误日志。正如您在错误日志中看到的那样,我“过度约束”了我的 UI。除了'yConstraint',我还有其他 4 个限制条件。我相信约束在一个 tuple 中,因此它们周围的括号。 XCode 试图通过提示“(注意:如果您看到不理解的 NSAutoresizingMaskLayoutConstraints,请参阅 UIView 属性 translatesAutoresizingMaskIntoConstraints 的文档)”来提供帮助,但我个人认为这没有足够的帮助。
什么是“自动调整大小蒙版”?
我想知道这意味着什么很重要,因为它被转换为约束,因此命名为translatesAutoresizingMaskIntoConstraints。
它是 UIView 的一个实例属性,包含一个整数位掩码。它保留了一些位,您可以打开和关闭“灵活左边距”和“灵活高度”等功能(其中更多here)。
当视图的边界发生变化时,该视图会根据每个子视图的自动调整大小掩码自动调整其子视图的大小。
source
总结一下自动调整蒙版,它保留了您想要的自动调整大小功能,例如灵活的高度和宽度。