How does Auto Layout resolve constraints?
understanding constraint-based layout.
Auto Layout solves a system of prioritized linear equations; intrinsicContentSize is a view's natural size; hugging resists growing, compression resistance resists shrinking.
What's really being asked
This verifies that you understand Auto Layout as a linear constraint solver and that you can reason about ambiguous or conflicting layouts using priorities rather than guessing. It separates engineers who fight the layout engine from those who direct it.
The full answer
Auto Layout expresses each constraint as a linear equation or inequality of the form attribute equals multiplier times another attribute plus a constant, each carrying a priority from 1 to 1000. The engine, built on a Cassowary-style solver, finds values for every view's frame that satisfy all required constraints, which have priority 1000, and best satisfies optional ones, breaking lower-priority constraints when the system is overdetermined. intrinsicContentSize is the size a view naturally wants given its content, such as a label sized to its text or a button sized to its title, so you need not pin both width and height explicitly. When a view could be larger or smaller than its intrinsic size, two priorities arbitrate. Content hugging priority resists growing beyond the intrinsic size, pulling edges inward. Compression resistance priority resists shrinking below it, pushing back against clipping.
The mistakes people make
Swapping the definitions, saying hugging prevents shrinking. Believing all constraints are absolute and conflicts crash. Ignoring that priorities decide which view stretches or truncates.
What usually comes next
What happens when two required constraints conflict? Which view stretches when two labels share a row? How do you debug an ambiguous layout? What priority value is required versus optional?
A concrete example
Two labels sit side by side in a fixed-width row. To make the second label truncate while the first shows fully, raise the first label's compression resistance above the second's. To decide which label hugs its text and which expands to fill slack, lower the hugging priority of the one that should stretch, so the solver enlarges it while the other stays at intrinsic width.
Interview question
Two adjacent labels in a row do not fit together. You want the first to stay fully visible and the second to truncate. Which adjustment achieves this?
- a.Raise the first label's compression resistance priority above the second'sCorrect
- b.Raise the second label's content hugging priority above the first's
- c.Lower the first label's intrinsicContentSize to zero
- d.Set both labels' constraints to priority 1000 so neither truncates
Why? this is the answer
Compression resistance resists shrinking below intrinsic size, so giving the first label higher compression resistance forces the second to truncate. Hugging governs growth, not truncation, and making both required would create an unsatisfiable conflict.
Just read this? Test yourself on what you have been reading.
Read the original → developer.apple.com
Put your scrolling time to good use
Learn one idea, try a quiz and save useful cards for revision. Tezvyn makes it easy to learn and stay current in your tech field, a few minutes at a time.
The iPhone app is on the way
We are building it. Until it lands, nothing here is held back from you: every interview card, your saved cards, streaks and the job board all work in Safari, plus hundreds of free practice quizzes of thirty questions each. Sign in and it all carries over to the app the day it arrives.
Want it as an icon? Tap Share at the bottom of Safari, then Add to Home Screen. It opens full screen and the cards you have read stay available offline.
We are hiring for this. Open roles that interview on ios — each one lists the topics its interview covers.
See open roles