Contents

ViewBuilder

A custom parameter attribute that constructs views from closures.

Declaration

@resultBuilder struct ViewBuilder

Mentioned in

Overview

When you build your project in Xcode 26 and earlier, use ViewBuilder as a parameter attribute for view-producing closure parameters, allowing those closures to provide multiple child views. For example, the following contextMenu function accepts a closure that produces one or more views via the view builder.

func contextMenu<MenuItems: View>(
    @ViewBuilder menuItems: () -> MenuItems
) -> some View

Clients of this function can use multiple-statement closures to provide several child views, as the following example shows:

myView.contextMenu {
    Text("Cut")
    Text("Copy")
    Text("Paste")
    if isSymbol {
        Text("Jump to Definition")
    }
}

When you build in Xcode 27 and later for any version of SwiftUI, the system constructs type-agnostic content from ViewBuilder closures, and doesn’t restrict the types you use in closures to conform to View. Mark closures with the type alias ContentBuilder instead to indicate where your code expects this behavior. For more information, see ContentBuilder.

Topics

Building content

Conditionally building content

See Also

Creating a view