apple-ux-guide

Layout

Auto Layout vs. Frame-Based Layout

Convention

Using Auto Layout with declarative constraints is usually preferred to frame-based layout (explicitly setting properties like frame and autoresizingMask).

Rationale

Using AutoLayout instead of frame-based layout will help your code automatically adapt to different configurations and OS capabilities like:

Examples

// Good: Auto Layout will properly mirror the layout for right-to-left languages
NSLayoutConstraint.activate([subview.leadingAnchor.constraint(equalTo: superview.leadingAnchor,
                                                             constant: leadingPadding)])
// Bad: Frame-based layout requires special computation for right-to-left languages
let superviewSize = superview.bounds.size
let subviewOriginX = traitCollection.layoutDirection == .rightToLeft
                    ? superviewSize.width - leadingPadding - superviewSize.width 
                    : leadingPadding
subview.frame = CGRect(x: subviewOriginX,
                        y: 0.0,
                        width: superviewSize.width,
                        height: superviewSize.height)

Exceptions

Auto Layout performance is sufficient for most experiences, even those with significant complexity. But for some layouts with an extremely large number of constraints active or complex layouts where the constraints are adjusted often based on current layout (multi-pass layouts), using frame-based layout may yield performance gains.

Examples where you might want to consider manual layout:

Compose Hierarchies with NSStackViews/UIStackViews

Use Layout Anchor Constraints