0Pricing
SwiftUI Academy · Lesson

ViewBuilder & Generic Containers

Accept arbitrary content with @ViewBuilder.

ViewBuilder & Generic Containers is a free SwiftUI Academy lesson on CoddyKit — lesson 4 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.

Containers That Hold Anything

VStack and List accept whatever views you nest inside them. You can build your own container that holds arbitrary content too. 📦

What @ViewBuilder Does

The @ViewBuilder attribute lets a closure list several views without commas or arrays. It is the magic behind stacks.

VStack {
    Text("One")
    Text("Two")
}

Accept content as a Closure

Make your container take a content closure marked @ViewBuilder. Callers then pass views just like they do for VStack.

struct Card<Content: View>: View {
    @ViewBuilder var content: Content
}

Generic over Content

The container is generic over its content type. That single struct can wrap a Text, an image, or a whole layout.

Render the Content

In your body, place the stored content inside whatever wrapper you want, like padding and a background.

var body: some View {
    content.padding().background(.gray)
}

Use Your Container

Call it with a trailing closure and nest any views inside. Your custom wrapper now feels just like a built-in one.

Card {
    Text("Title")
    Text("Body")
}

Builder on Functions Too

Mark a helper function with @ViewBuilder to return different views from branches without wrapping them in a type.

@ViewBuilder
func footer(show: Bool) -> some View {
    if show { Text("More") } else { EmptyView() }
}

Conditionals in Builders

Inside a builder you can use if and switch freely. SwiftUI assembles the chosen branch into the final view tree.

Multiple Content Slots

A container can take more than one builder, like a header and a footer. Each slot is its own @ViewBuilder closure.

struct Panel<H: View, B: View>: View {
    @ViewBuilder var header: H
    @ViewBuilder var body2: B
}

Reusable Layout Shells

Generic containers capture a layout shell once, like a bordered card, and let every screen fill it with unique content.

Type-Safe and Flexible

Because content is generic, the compiler still checks every view. You get flexibility without losing type safety.

Quick Check

Quick check on generic containers.

Recap: Build Flexible Containers

Combine @ViewBuilder with a generic content type to make containers that wrap any views. Reusable shells, full type safety. 🧠

Frequently asked questions

Is the “ViewBuilder & Generic Containers” lesson free?

Yes — the full text of “ViewBuilder & Generic Containers” 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 “ViewBuilder & Generic Containers”?

Accept arbitrary content with @ViewBuilder. 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 4 of 4, so you can start here or from the beginning and move at your own pace.

How long does the “ViewBuilder & Generic Containers” 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. Extracting Reusable Views
  2. Custom ViewModifiers
  3. ButtonStyle & LabelStyle
  4. ViewBuilder & Generic Containers
← Back to SwiftUI Academy