skip to content

In UIKit, a trip card prints 'Unable to simultaneously satisfy constraints' to the console; how do you read that log and decide which constraint to make optional?

level: seniorimportance: must knowfreq 66%

answer

  1. no solution, not too many
  2. only required constraints can clash
  3. identifiers turn addresses into names
  4. UIKit breaks one and carries on
  5. 999 set before activation

basics

~20 s

The log lists required constraints that cannot all hold and names the one UIKit breaks to recover. Add up one axis, find the constraint that is really a preference, and give it an optional priority before activating it.

solid answer

~50 s

The message means the **required** constraints have no solution; optional ones cannot cause it, because the solver may leave them unmet. The log prints each constraint in the conflict, such as `'tripCard.fare.width' UILabel:0x….width == 90`, so giving constraints an `identifier` makes it readable. Add the numbers along one axis: 16 + 220 + 8 + 90 + 16 needs 350 points on a card fixed at 320. UIKit then says *Will attempt to recover by breaking constraint*, names one, and keeps running; its choice may not be yours, so the screen can look fine by luck. The fix is to decide which constraint is a wish rather than a law, here the name's width, and give it an optional priority **before** activating it, since Apple's documentation says switching an installed constraint between required and optional throws. A symbolic breakpoint on `UIViewAlertForUnsatisfiableConstraints` stops the debugger at the moment of the conflict.

code

plaintext · 19 lines
plaintext
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:0x7a6ad4c640 'tripCard.fare.leading' H:[UILabel:0x7a6adb01c0]-(8)-[UILabel:0x7a6adb0380]   (active)>",
    "<NSLayoutConstraint:0x7a6ad4c6e0 'tripCard.fare.trailing' UILabel:0x7a6adb0380.trailing == UIView:0x7a6adb0000.trailing - 16   (active)>",
    "<NSLayoutConstraint:0x7a6ad4c690 'tripCard.fare.width' UILabel:0x7a6adb0380.width == 90   (active)>",
    "<NSLayoutConstraint:0x7a6ad4c5a0 'tripCard.name.leading' H:|-(16)-[UILabel:0x7a6adb01c0]   (active, names: '|':UIView:0x7a6adb0000 )>",
    "<NSLayoutConstraint:0x7a6ad4c5f0 'tripCard.name.width' UILabel:0x7a6adb01c0.width == 220   (active)>",
    "<NSLayoutConstraint:0x7a6ad4c4b0 'tripCard.width' UIView:0x7a6adb0000.width == 320   (active)>"
)

Will attempt to recover by breaking constraint
<NSLayoutConstraint:0x7a6ad4c5f0 'tripCard.name.width' UILabel:0x7a6adb01c0.width == 220   (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.

go deeper

for a junior

Recall that the log means required constraints contradict each other and that UIKit breaks one of them to keep going.

for a middle

Explain the difference between conflicting and ambiguous layouts, and why optional priorities never appear as the cause.

for a senior

Read a real log under pressure: name constraints with identifiers, add up the axis, spot autoresizing-mask constraints, and choose which rule becomes a wish.

for a principal

Push for treating every conflict log as a defect, with constraint identifiers and smallest-width checks as part of the team's layout review.

## Two kinds of layout error Apple's documentation defines a valid layout as constraints with **exactly one** solution. There are two ways to miss it: - **Ambiguous**: more than one solution. Nothing is logged; the header says UIKit does not detect ambiguity automatically, and the symptom is views that jump or sit in the wrong place. - **Conflicting** (unsatisfiable): no solution. UIKit detects this while solving and prints *Unable to simultaneously satisfy constraints* to the console. A conflict can only involve **required** constraints, priority 1,000. An optional constraint the solver cannot meet is simply satisfied as closely as possible, which is why a label's compression resistance at 750 never produces this message on its own. ## Reading the log After the heading and some advice, the log prints the constraints that cannot hold together. Each line is a constraint's description: - its address, and its `identifier` in quotes if you gave it one; - the equation, such as `UILabel:0x….width == 90` or `UILabel:0x….trailing == UIView:0x….trailing - 16`; - spacing written in visual-format style, such as `H:|-(16)-[UILabel:0x…]` for an edge to the superview or `H:[UILabel:0x…]-(8)-[UILabel:0x…]` between two views; - the state, `(active)`. Then come three more lines: *Will attempt to recover by breaking constraint* with the one UIKit chose, a suggestion to make a symbolic breakpoint at `UIViewAlertForUnsatisfiableConstraints`, and a pointer to the debugging methods on `UIView`. Work through it in this order: 1. **Find your views.** Memory addresses are hard to match; an `identifier` such as `"tripCard.fare.width"` makes each line self-explanatory. Apple reserves identifiers starting with `NS` (the header adds `UI`). 2. **Check for autoresizing-mask constraints.** Constraints that UIKit generated from a view's frame and autoresizing mask mean a view you constrain still has `translatesAutoresizingMaskIntoConstraints == true`. Setting it to `false` usually ends the conflict. 3. **Add up one axis.** For the trip card: 16 leading + name 220 + 8 gap + fare 90 + 16 trailing = 350 points, on a card fixed at 320. No arrangement of required constraints can satisfy that. 4. **Decide which constraint is a preference.** The fare's width and the margins are design rules; the name's width is a wish for wide cards. 5. **Make that one optional, or delete it if it duplicates another.** ## Why "it still looks right" is not a fix After printing the log, UIKit breaks the constraint it named and lays out the rest. Which one it breaks is UIKit's choice, not yours, and it can differ when the set of constraints changes. A screen that looks right after a conflict looks right by accident; on another device, text size or language it can break differently. Treat every conflict log as a bug. ## Choosing the priority | Priority | Meaning in this fix | |---|---| | 1000 (`.required`) | must hold; a contradiction among these is what the log reports | | 999 | optional, yet above every named system level, so it holds whenever it can | | 750 (`.defaultHigh`) | the level of a label's compression resistance, so the two trade off | | 250 (`.defaultLow`) | yields to almost everything, including a hugging priority of 251 | Set the priority **before** the constraint is activated. Apple's documentation says an exception is thrown if an installed constraint changes from required to optional or back, while changing one optional value to another is allowed at any time. That is why teams that may need to relax a constraint later create it at 999 from the start. ## Tools beside the log UIKit's `UIConstraintBasedLayoutDebugging` category on `UIView` helps with both kinds of error; the header says these methods should be used in debugging only, never in shipping code: - `constraintsAffectingLayout(for:)` lists the constraints that affect a view's position and size on one axis. - `hasAmbiguousLayout` reports whether another frame would satisfy the same constraints. - `exerciseAmbiguityInLayout()` moves an ambiguous view to another valid layout, so you can see what is missing. A conflict that appears only on the smallest iPhone or at the largest text size is a sign the layout was designed around one width; it is worth adding that configuration to every layout review.

  • Why does lowering the name's width to 750 end the conflict?
    At 1,000 the name's width was required, and the required set had no solution. At 750 it is optional: the solver meets every required constraint first, which leaves the name 190 points, and comes as close to 220 as it can. The layout now has a solution, so no log is printed.
  • How do you tell a conflicting layout from an ambiguous one?
    A conflict prints the Unable to simultaneously satisfy constraints log, because the required constraints have no solution. Ambiguity prints nothing; views jump or sit in odd places. In a debug session, `hasAmbiguousLayout` and `exerciseAmbiguityInLayout()` on the suspect view reveal ambiguity, while the log and `constraintsAffectingLayout(for:)` explain a conflict.
  • What does the symbolic breakpoint the log suggests give you?
    A breakpoint on `UIViewAlertForUnsatisfiableConstraints` pauses the debugger when UIKit reports the conflict, so the backtrace shows what code was running, for example the layout pass after a particular update. That helps when the log alone does not say which change introduced the bad constraint.

  • If the screen looks right after the log, the conflict can be ignored.
  • Two optional priorities at 750 are enough to cause this conflict log.
  • Lowering an installed required constraint to 999 is always safe.
  • UIKit always breaks the most recently added constraint.
  • Ambiguous layouts print the same log as conflicting ones.