<!--
{
  "availability" : [
    "iOS: 13.0.0 -",
    "iPadOS: 13.0.0 -",
    "macCatalyst: 13.0.0 -",
    "macOS: 10.15.0 -",
    "tvOS: 13.0.0 -",
    "visionOS: 1.0.0 -",
    "watchOS: 6.0.0 -"
  ],
  "documentType" : "symbol",
  "framework" : "SwiftUI",
  "identifier" : "/documentation/SwiftUI/ContentBuilder",
  "metadataVersion" : "0.1.0",
  "role" : "Type Alias",
  "symbol" : {
    "kind" : "Type Alias",
    "modules" : [
      "SwiftUI"
    ],
    "preciseIdentifier" : "s:7SwiftUI14ContentBuildera"
  },
  "title" : "ContentBuilder"
}
-->

# ContentBuilder

A custom parameter attribute that constructs views and other content types
from closures.

```
typealias ContentBuilder = ViewBuilder
```

## Discussion

You apply `ContentBuilder`, a type alias for [`ViewBuilder`](/documentation/SwiftUI/ViewBuilder), as a
parameter attribute to closure parameters, computed properties, or protocol
requirements. Then, SwiftUI builds content from multiple statements in the
closures you provide. For example, the following `contextMenu` function
accepts a closure that produces one or more views from the content builder.

```swift
func contextMenu<MenuItems: View>(
    @ContentBuilder menuItems: () -> MenuItems
) -> some View
```

You can use multiple-statement closures to provide several subviews, as
the following example shows:

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

You can also use `ContentBuilder` with other SwiftUI-style result builders.
For example, the following computed property iterates over cases in an
enumeration to produce toolbar items.

```swift
@ContentBuilder
var editingToolbarItems: some ToolbarContent {
    ForEach(EditingOptions.toolbarItems, id: \.self) { editingOption in
        ToolbarItem {
            Button(editingOption.title) {
                editingOption.action()
            }
        }
    }
}
```

SwiftUI constructs type-agnostic content from closures that you mark with
`ContentBuilder`, which serves as the unified replacement for type-specific
builders like [`ToolbarContentBuilder`](/documentation/SwiftUI/ToolbarContentBuilder) and [`CommandsBuilder`](/documentation/SwiftUI/CommandsBuilder).

In its build functions, `ContentBuilder` doesn’t enforce protocol
conformance. Instead, it maintains type safety through conditional
conformances on the content types it produces. For example,
[`TupleContent`](/documentation/SwiftUI/TupleContent) conditionally conforms to content types based on which
types the content items it contains conform to. This allows a single,
shared set of initializers on [`Group`](/documentation/SwiftUI/Group), [`ForEach`](/documentation/SwiftUI/ForEach), and [`Section`](/documentation/SwiftUI/Section) to
serve all content types, rather than a separate overloaded initializer per
builder.

---

Copyright &copy; 2026 Apple Inc. All rights reserved. | [Terms of Use](https://www.apple.com/legal/internet-services/terms/site.html) | [Privacy Policy](https://www.apple.com/privacy/privacy-policy)