With PANKAJ VERMA
's answer, it finally makes sense.
In my case, I have an ImageView
and only set 2 constraints, yet the error shows that I have more constraints and LayoutConstraints was Unable to simultaneously satisfy constraints:
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"
Error message:
[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.
There was basically an almost identical error log for the 2nd constraint ("xConstraint"
) I set. As you can see in the error logs, I am "over-constraining" my UI. Along with 'yConstraint'
, I also have 4 other constraints. I believe the constraints are in a tuple, hence the paranthesis around them. XCode tries to be helpful by hinting "(Note: If you're seeing NSAutoresizingMaskLayoutConstraints that you don't understand, refer to the documentation for the UIView property translatesAutoresizingMaskIntoConstraints)", but personally I don't think this is helpful enough.
What is "Autoresizing Mask"?
I guess its important to know what this means, as this is being converted to constraints, hence the name translatesAutoresizingMaskIntoConstraints
.
Its an instance property of UIView containing an integer bit mask. It holds onto bits that you can toggle on and off for features like "flexible left margin" and "flexible height" (more of them here).
When a view’s bounds change, that view automatically resizes its subviews according to each subview’s autoresizing mask.
source
So to summarize the Autoresizing Mask, it holds onto the autoresize features you want, like flexible height and width.