iOS Development
Auto Layout
Visual Format Language
UIView
Interface Builder

Centering a view in its superview using Visual Format Language

Master System Design with Codemia

Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.

Introduction

Visual Format Language, or VFL, is useful for describing horizontal and vertical edge relationships, but it does not have a native syntax for center alignment. That is the key fact behind this topic: you cannot fully express centerX and centerY constraints using VFL alone. The normal solution is to combine VFL for size or spacing with explicit center constraints.

Why VFL Cannot Directly Express Centering

VFL is built around edge-to-edge relationships. It is good at expressing layouts such as:

  • pin this view to the left and right edges
  • stack these views vertically
  • keep a fixed gap between views

What it does not express directly is:

  • 'subview.centerX == superview.centerX'
  • 'subview.centerY == superview.centerY'

That means if your goal is exact centering, you usually write a couple of normal NSLayoutConstraint constraints alongside any VFL constraints.

Typical Hybrid Solution

A common approach is:

  • use VFL to set width and height
  • use regular constraints for center X and center Y
swift
1import UIKit
2
3final class ExampleViewController: UIViewController {
4    override func viewDidLoad() {
5        super.viewDidLoad()
6
7        let box = UIView()
8        box.translatesAutoresizingMaskIntoConstraints = false
9        box.backgroundColor = .systemBlue
10        view.addSubview(box)
11
12        let views = ["box": box]
13
14        view.addConstraints(
15            NSLayoutConstraint.constraints(
16                withVisualFormat: "H:[box(120)]",
17                options: [],
18                metrics: nil,
19                views: views
20            )
21        )
22
23        view.addConstraints(
24            NSLayoutConstraint.constraints(
25                withVisualFormat: "V:[box(80)]",
26                options: [],
27                metrics: nil,
28                views: views
29            )
30        )
31
32        view.addConstraint(
33            NSLayoutConstraint(
34                item: box,
35                attribute: .centerX,
36                relatedBy: .equal,
37                toItem: view,
38                attribute: .centerX,
39                multiplier: 1,
40                constant: 0
41            )
42        )
43
44        view.addConstraint(
45            NSLayoutConstraint(
46                item: box,
47                attribute: .centerY,
48                relatedBy: .equal,
49                toItem: view,
50                attribute: .centerY,
51                multiplier: 1,
52                constant: 0
53            )
54        )
55    }
56}

This is the classic answer: VFL handles the size, and normal constraints handle the centering.

Why Equal Edge Gaps Are Not a Good Substitute

Developers sometimes try to simulate centering by writing equal left and right spacing constraints in VFL. That can work only if the view already has a known size and the surrounding layout is simple. Even then, it is usually less clear than just expressing center alignment directly.

For example, forcing equal margins on both sides may center the view mathematically, but it also ties the result to additional assumptions about width and available space. A direct center constraint communicates intent much more clearly.

Modern Auto Layout APIs Are Usually Better

Although VFL is still supported, anchor-based constraints are often easier to read and maintain today:

swift
1box.widthAnchor.constraint(equalToConstant: 120).isActive = true
2box.heightAnchor.constraint(equalToConstant: 80).isActive = true
3box.centerXAnchor.constraint(equalTo: view.centerXAnchor).isActive = true
4box.centerYAnchor.constraint(equalTo: view.centerYAnchor).isActive = true

This is one reason many modern UIKit codebases use anchors instead of VFL except in a few compact edge-layout cases.

When VFL Still Makes Sense

VFL remains handy when you want to generate repetitive edge constraints concisely, such as a stacked form or equally sized horizontal row. But when the layout intent is primarily about alignment rather than edge relationships, center constraints or anchors are clearer.

That does not make VFL obsolete. It just means centering is not its strongest use case.

Common Pitfalls

  • Expecting VFL to have a direct centerX or centerY syntax.
  • Trying to fake centering with equal edge gaps and making the layout harder to understand.
  • Forgetting to disable translatesAutoresizingMaskIntoConstraints before adding constraints.
  • Using VFL for everything even when anchors would express intent more clearly.
  • Setting size constraints but forgetting the actual position constraints.

Summary

  • VFL does not directly express centering constraints.
  • The standard solution is to combine VFL with explicit centerX and centerY constraints.
  • Equal edge spacing can approximate centering, but it is usually less clear.
  • Anchor-based Auto Layout is often easier than VFL for modern UIKit code.
  • Use the tool that expresses the layout intent most directly.

Course illustration
Course illustration

All Rights Reserved.