How to Simultaneously Satisfy Constraints, Will Attempt to Recover by Breaking Constraint

Will attempt to recover by breaking constraint (but not sure why my constraints are wrong)

Change this part

    contentsView.translatesAutoresizingMaskIntoConstraints = false
NSLayoutConstraint.activate([
contentsView.leadingAnchor.constraint(equalTo: layoutMarginsGuide.leadingAnchor),
contentsView.trailingAnchor.constraint(equalTo: layoutMarginsGuide.trailingAnchor),
contentsView.topAnchor.constraint(equalTo: layoutMarginsGuide.topAnchor),
contentsView.bottomAnchor.constraint(equalTo: layoutMarginsGuide.bottomAnchor),
contentsView.heightAnchor.constraint(equalToConstant: 60)
])

to

contentsView.translatesAutoresizingMaskIntoConstraints = false

let con = contentsView.bottomAnchor.constraint(equalTo: layoutMarginsGuide.bottomAnchor)
con.priority = UILayoutPriority(rawValue: 999)
NSLayoutConstraint.activate([
contentsView.leadingAnchor.constraint(equalTo: layoutMarginsGuide.leadingAnchor),
contentsView.trailingAnchor.constraint(equalTo: layoutMarginsGuide.trailingAnchor),
contentsView.topAnchor.constraint(equalTo: layoutMarginsGuide.topAnchor),
con,
contentsView.heightAnchor.constraint(equalToConstant: 60)
])

Unable to simultaneously satisfy constraints. Probably at least one of the constraints

It's incredibly difficult to solve these problems or warnings.

Here is ONE KEY TIP.

You can actually add a name to EVERY! constraint.

Sample Image

Do so.

Make the name clear and obvious like "the top constraint on the second box" or whatever.

You don't have to do every single constraint in the app, just the possibly problem ones in the relevant region.

Before anything else, you have to do that. :/

Note that there is a vast amount of discussions about this on this site, 100s of questions, eg https://stackoverflow.com/a/31833436/294884

Essentially, "your constraints have a minor problem" and specifically you likely have "one too many constraints somewhere" (or more subtly, you have to change the priority on one).

Your first step is the naming trick, to at least find out what the hell constraints Apple is referring to :/


Some suggest this BTW! http://wtfautolayout.com



For this particular question:

In this particular case, of the many, many possible problems, it turns out the OP's problem was the "two constraints" issue:

If you DO have/need two constraints, then ONE of them MUST have a DIFFERENT priority than the other.

Getting Warning for UI Layout in iOS Unable to simultaneously satisfy constraints

This is a common situation when using cells with embedded views.

As you've seen, the UI result looks fine -- because auto-layout will attempt to recover by breaking constraint, and then it re-establishes the constraint.

To get rid of the warnings, give the constraint that ends up defining the height a less-than-required priority.

So, change the end of your setupView() func to this:

    if let lastView = contentHolderView.getTheLastAddedView() {
let c = lastView.bottomAnchor.constraint(equalTo: contentHolderView.bottomAnchor, constant: -20)
c.priority = .required - 1
c.isActive = true
//lastView.bottomAnchor.constraint(equalTo: contentHolderView.bottomAnchor, constant: -20).isActive = true
}

// this is not needed
//self.layoutIfNeeded()

As a side note, a cell's subviews should not be added to, or constrained to, the cell itself. You should use the cell's .contentView instead:

    contentView.addSubview(contentHolderView)

and then:

    NSLayoutConstraint.activate([
//layout contentView
contentHolderView.topAnchor.constraint(equalTo: contentView.topAnchor,constant: 0),
contentHolderView.bottomAnchor.constraint(equalTo: contentView.bottomAnchor,constant: 0),
contentHolderView.leadingAnchor.constraint(equalTo: contentView.leadingAnchor,constant: 0),
contentHolderView.trailingAnchor.constraint(equalTo: contentView.trailingAnchor,constant: 0),

swiftui - how to avoid 'Unable to simultaneously satisfy constraints' error?

Let's look at these one by one:

This is saying view 0x7fde585064a0 is active and has 44 points height

This is saying that 0x7fde585064a0's top edge must be a gap of 0 points from the top of its superview 0x7fde58518070

This is saying view 0x7fde51b78c50 and 0x7fde585064a0 are conflicting. These four things can't all be true. And the problem with constraints 0x600002591b30. Find out where the problem is and set true constraint.

I think it's useful to know the basics and understand what Apple / Xcode is trying to tell you via logs:

H = Horizontal constraint(for leading and Trailing)
V = Vertical constraint(top and bottom edge)
h = height
w = width

TopEdge -> V:|-(points)-[VIEW:memoryAddress]
BottomEdge -> V:[VIEW:memoryAddress]-(points)-|
Leading -> H:|-(points)-[VIEW:memoryAddress]
Trailing -> H:[VIEW:memoryAddress] -(points)-|
height -> h= --& v=--& V:[VIEW:memoryAddress((points)]
width -> VIEW:memoryAddress.width == points
between -> H:[VIEW 1]-(51)-[VIEW 2]

Unable to simultaneously satisfy constraints with animation

The reason that you're seeing this is because you are activating new (conflicting) constraints before disabling the old ones that are already in effect.

The start of your else block is the problem, you need to disable the others first before setting tennisButtonHiddenConstraint1 and tennisButtonHiddenConstraint2 to active:

    self.tennisButtonBottomAnchor?.isActive = false
self.tennisButtonRightAnchor?.isActive = false
self.tennisButtonHeightAnchor?.isActive = false
self.tennisButtonWidthAnchor?.isActive = false
self.tennisButtonHiddenConstraint1?.isActive = true
self.tennisButtonHiddenConstraint2?.isActive = true

It works on first load because no other constraints have been added yet (so the order doesn't matter).

If in doubt, always set .isActive = false first is my rule of thumb when toggling between sets of constraints.

LayoutConstraints - Unable to simultaneously satisfy constraints

Step 1:

Drag a View to your cell and apply horizontally and vertically center constraints to to your view (This will show few constraint breaks but hang on a bit)

Sample Image

Step 2:

Drag imageView to view and apply height constraint/ aspect ratio/ leading / height and bottom constraint as shown

Sample Image

Step 3:

Drag UILabel to View preferably right side of ImageView and apply trailing constraint and leading constraint to label. Leading constraint will be to content view of cell while leading constraint to UIImageView.

Trailing Constraint to Label

Sample Image

Now add Leading constraint to UIImageView (Control drag from label to imageView and select horizontal spacing)

Sample Image

Finally control drag from label to imageView again and select center vertically

Sample Image

That should be it :)

If everything is fine final constraints should look like

Sample Image

Hope this helps



Related Topics



Leave a reply



Submit