Fix: Unexpected Constraint Offsets from Constrain to Margins in Xcode Storyboard

Fix: Unexpected Constraint Offsets from Constrain to Margins in Xcode Storyboard

Written by

in

Quick answer: The “Constrain to Margins” checkbox makes constraints reference a view’s layout margins (default 8pt inset) instead of its edges. When checked, Xcode suggests constraints offset by the margin (e.g., -16 for leading/trailing). Uncheck it to constrain directly to the view’s edges for edge-to-edge layouts.

Why this happens

When you add constraints in Interface Builder, the Constrain to Margins option (labeled “Relative to margin” in the constraint editor) determines whether a constraint anchors to a view’s layout margins or its edges.

Every UIView has default layout margins of 8 points on each side. These margins act like internal padding — similar to CSS padding — so content doesn’t sit flush against the view’s bounds. When Constrain to Margins is enabled (the default in Xcode 6 and later), Interface Builder creates constraints relative to this inset rectangle rather than the view’s frame.

That’s why a full-screen UITableView shows a suggested constant of -16 (8pt left margin + 8pt right margin) instead of 0. The constraint is pinning the table view’s edges to the superview’s margins, not its edges.

This feature was introduced in iOS 8 / Xcode 6. It does not crash on iOS 7 as long as you don’t modify layoutMargins or preservesSuperviewLayoutMargins in code. The margins simply behave like fixed 8pt insets on older systems.

How to fix it

1. Uncheck Constrain to Margins for edge-to-edge layouts

If you want a subview to fill its superview exactly (full-screen table view, background image, etc.), disable the margin option:

  1. Select the constraint in the Size Inspector or the constraint popover.
  2. Uncheck Relative to margin (or Constrain to Margins in the Add New Constraints sheet) for the superview side of the constraint.
  3. The constant updates to 0 for leading/trailing/top/bottom edges.

This is the most common fix. Use it whenever the design calls for content extending to the screen edges.

2. Keep Constrain to Margins for consistent inset spacing

When you want a standard system spacing around content, leave the option checked. All subviews constrained to the superview’s margins will share the same inset. If you later change the superview’s layoutMargins in code, every constrained subview updates automatically:

// Adjust all margin-constrained subviews at once
rootView.layoutMargins = UIEdgeInsets(top: 0, left: 20, bottom: 0, right: 20)

This is useful for text-heavy screens where you want readable line lengths without hard-coding constraint constants.

3. Mix margin and edge constraints per side

You can constrain the leading edge to the margin while pinning the trailing edge to the superview’s edge. In the constraint editor, each end of a constraint has its own “Relative to margin” checkbox:

  • First item (subview) unchecked, Second item (superview) checked → subview edge aligns to superview margin.
  • First item unchecked, Second item unchecked → subview edge aligns to superview edge.
  • First item checked, Second item unchecked → subview margin aligns to superview edge (subview overlaps).
  • Both checked → subview margin aligns to superview margin (same visual result as both unchecked when margins are equal).

Choose per-side based on whether that side should respect the system inset.

4. Override layout margins in code for dynamic spacing

If you need different margins per device size class, orientation, or dynamic type, set layoutMargins in viewDidLayoutSubviews or traitCollectionDidChange:

override func viewDidLayoutSubviews() {
    super.viewDidLayoutSubviews()
    let inset: CGFloat = traitCollection.horizontalSizeClass == .regular ? 40 : 16
    view.layoutMargins = UIEdgeInsets(top: 0, left: inset, bottom: 0, right: inset)
}

All constraints referencing the view’s margins will animate to the new values if changed inside an animation block.

How to prevent it

  • Default to unchecked for full-bleed backgrounds, scroll views, and edge-to-edge containers.
  • Enable only when you want Apple’s recommended readable-content margins (8pt default, respects safe area).
  • Verify in the canvas: Enable Editor > Canvas > Show Layout Rectangles to see margin outlines while designing.
  • Test on iOS 7 (if still supported) — margins act as fixed 8pt insets unless you touch layoutMargins in code.

FAQ

Does Constrain to Margins work with Safe Area Layout Guide?

Yes. The safe area and layout margins are separate concepts. Safe area avoids notches, home indicator, and bars. Layout margins provide internal padding. A constraint can reference the safe area and be relative to margins — the margin is applied inside the safe area.

Why can’t I change margin values in Interface Builder?

Interface Builder doesn’t expose a layoutMargins property for views. You must set it in code (view.layoutMargins = ...) or override layoutMarginsDidChange in a custom view subclass.

Should I use margins for all new projects?

Not necessarily. Margins are a tool for consistent inset management. If your design uses varying per-edge spacing or complex grids, explicit constraint constants are often clearer. Use margins when multiple sibling views should share the same horizontal or vertical inset and you want a single point of control.

Sources & credits

Parts of this guide are adapted from content licensed under CC BY-SA 4.0; this article is shared under the same license.