How to Fix iOS Auto Layout Constraint Error
In this tutorial, you'll learn about How to Fix iOS Auto Layout Constraint Error. We cover key concepts, practical examples, and best practices.
The Problem
Your app's UI looks broken and the console shows:
Unable to simultaneously satisfy constraints.
Will attempt to recover by breaking constraint
<NSLayoutConstraint:0x6000...>
Or:
(NSLayoutConstraint:0x6000... UIView:0x7f... leading == 20)
(NSLayoutConstraint:0x6000... UIView:0x7f... leading == 40)
The layout is ambiguous: constraints conflict or are insufficient to determine the view's position and size.
Quick Fix
Step 1: Identify the conflicting constraints
Xcode logs every broken constraint with a memory address. Use the View Hierarchy Debugger:
- Run the app
- Click the Debug View Hierarchy button in Xcode's debug bar
- Select the problematic view and inspect its constraints
Step 2: Check constraint priorities
// Lower priority for optional constraints
view.widthAnchor.constraint(equalToConstant: 200).priority = .defaultHigh
Constraints with priority required (1000) cannot be broken. Lower-priority constraints break first.
Step 3: Add missing constraints
Every view needs constraints for:
- X position (leading/trailing/centerX)
- Y position (top/bottom/centerY)
- Width or leading+trailing
- Height or top+bottom
Missing one of these produces "ambiguous layout."
Step 4: Use equal widths/heights for adaptive layouts
NSLayoutConstraint.activate([
redView.leadingAnchor.constraint(equalTo: view.leadingAnchor, constant: 16),
redView.topAnchor.constraint(equalTo: view.safeAreaLayoutGuide.topAnchor, constant: 16),
blueView.leadingAnchor.constraint(equalTo: redView.trailingAnchor, constant: 8),
blueView.topAnchor.constraint(equalTo: redView.topAnchor),
blueView.widthAnchor.constraint(equalTo: redView.widthAnchor),
blueView.trailingAnchor.constraint(equalTo: view.trailingAnchor, constant: -16),
redView.heightAnchor.constraint(equalToConstant: 100),
blueView.heightAnchor.constraint(equalTo: redView.heightAnchor)
])
Step 5: Set translatesAutoresizingMaskIntoConstraints
redView.translatesAutoresizingMaskIntoConstraints = false
blueView.translatesAutoresizingMaskIntoConstraints = false
Views created in code need this set to false. Views from Interface Builder have it set automatically.
Step 6: Use UIStackView for simpler layouts
let stackView = UIStackView(arrangedSubviews: [label, button, textField])
stackView.axis = .vertical
stackView.spacing = 8
stackView.translatesAutoresizingMaskIntoConstraints = false
view.addSubview(stackView)
NSLayoutConstraint.activate([
stackView.centerXAnchor.constraint(equalTo: view.centerXAnchor),
stackView.centerYAnchor.constraint(equalTo: view.centerYAnchor)
])
StackView manages internal constraints automatically.
Step 7: Fix Interface Builder constraints
In Storyboard or XIB, select the view and click Resolve Auto Layout Issues (the triangle icon in the bottom-right). Choose Update Constraints or Add Missing Constraints.
Prevention
- Always set
translatesAutoresizingMaskIntoConstraints = falsefor code-created views. - Use the View Hierarchy Debugger before shipping.
- Prefer
UIStackViewover manual constraints for linear layouts.
Common Mistakes with autolayout error
- Using
foldlinstead offoldl'causing stack overflow on large lists - Forgetting
deriving (Show, Eq)on custom data types needed for debugging - Placing the wildcard pattern first in case expressions, making all subsequent patterns unreachable
These mistakes appear frequently in real-world IOS code. DodaTech's contributors have identified these patterns through analysis of open-source projects and production systems.
Practice Exercise
Write a pure function that safely divides two integers using Maybe, then test it with edge cases like division by zero and negative numbers.
This exercise reinforces the concepts covered in this guide. Try implementing it before checking online solutions.
FAQ
DodaTech Tool Reference
Doda Browser's Layout Inspector provides real-time constraint visualization for debugging Auto Layout issues directly on a connected device.
Built by the developers of DodaTech
Doda Browser, DodaZIP & Durga Antivirus Pro