0Pricing
SwiftUI Academy · Lesson

Custom Alignment Guides

Align views across the hierarchy precisely.

Custom Alignment Guides is a free SwiftUI Academy lesson on CoddyKit — lesson 2 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the SwiftUI Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.

Lining Things Up

Stacks align children using a shared edge or center. Alignment guides let you fine-tune exactly where that line falls. 📏

Built-in Alignments

An HStack takes a vertical alignment like .top or .center, while a VStack takes a horizontal one.

HStack(alignment: .top) {
    Text("A")
    Text("Big")
}

The alignmentGuide Modifier

The alignmentGuide modifier overrides where a single view exposes a given alignment to its parent.

Text("Hi")
    .alignmentGuide(.top) { d in d[.bottom] }

Reading Dimensions

The closure gets a ViewDimensions value, letting you read d.width, d.height, or any named guide.

Text("Hi").alignmentGuide(.leading) { d in
    d.width / 2
}

Shift the Guide

Returning a different number moves the view relative to its peers, since the parent lines up everyone on that returned value.

Custom Alignment ID

For reuse, define a type conforming to AlignmentID with a defaultValue for unaligned cases.

struct MidBadge: AlignmentID {
    static func defaultValue(in d: ViewDimensions) -> CGFloat {
        d[.bottom]
    }
}

Name It on Alignment

Extend VerticalAlignment or HorizontalAlignment with a static property bound to your AlignmentID.

extension VerticalAlignment {
    static let midBadge = VerticalAlignment(MidBadge.self)
}

Use the Custom Line

Pass your named alignment to a stack, then tag the views that should snap to that custom line.

HStack(alignment: .midBadge) {
    Avatar()
    Label()
}

Tag the Participants

Each view that should share the line sets alignmentGuide for your custom alignment to the point it wants to match.

Label()
    .alignmentGuide(.midBadge) { d in
        d[VerticalAlignment.center]
    }

Cross-Hierarchy Power

Custom guides shine when views live in different containers but must still align, like a caption to an icon center across columns. ✨

Mind the Default

Any view that does not set your custom guide falls back to its defaultValue, so pick that default thoughtfully.

Quick Check

Recall what the alignmentGuide closure receives.

Recap

You can now override alignment with alignmentGuide and define custom AlignmentID types to align views across the hierarchy. 🎉

Frequently asked questions

Is the “Custom Alignment Guides” lesson free?

Yes — the full text of “Custom Alignment Guides” is free to read here on the web, and the SwiftUI Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the SwiftUI Academy course, upgrade to CoddyKit PRO.

What will I learn in “Custom Alignment Guides”?

Align views across the hierarchy precisely. You practise SwiftUI Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.

Do I need any experience to start SwiftUI Academy?

No prior experience is required. SwiftUI Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 2 of 4, so you can start here or from the beginning and move at your own pace.

How long does the “Custom Alignment Guides” lesson take?

Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.

Can I write and run code in this SwiftUI Academy lesson?

Yes. Every SwiftUI Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.

All lessons in this course

  1. GeometryReader Essentials
  2. Custom Alignment Guides
  3. The Layout Protocol
  4. Building a Flow Layout
← Back to SwiftUI Academy