<!--
{
  "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/ViewBuilder",
  "metadataVersion" : "0.1.0",
  "role" : "Structure",
  "symbol" : {
    "kind" : "Structure",
    "modules" : [
      "SwiftUI"
    ],
    "preciseIdentifier" : "s:7SwiftUI11ViewBuilderV"
  },
  "title" : "ViewBuilder"
}
-->

# ViewBuilder

A custom parameter attribute that constructs views from closures.

```
@resultBuilder struct ViewBuilder
```

## Overview

When you build your project in Xcode 26 and earlier, use [`ViewBuilder`](/documentation/SwiftUI/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`](/documentation/SwiftUI/View). Mark
closures with the type alias [`ContentBuilder`](/documentation/SwiftUI/ContentBuilder) instead to indicate where
your code expects this behavior. For more information, see
[`ContentBuilder`](/documentation/SwiftUI/ContentBuilder).

## Topics

### Building content

[`static   buildBlock ()`](/documentation/SwiftUI/ViewBuilder/buildBlock())

Builds an empty content from a block containing no statements.

[`static   buildBlock ( _ :)`](/documentation/SwiftUI/ViewBuilder/buildBlock(_:))

Passes a single piece of content written as a child view through unmodified.

### Conditionally building content

[`buildEither(first:)`](/documentation/SwiftUI/ViewBuilder/buildEither(first:))

Produces content for a conditional statement in a multi-statement closure
when the condition is true.

[`buildEither(second:)`](/documentation/SwiftUI/ViewBuilder/buildEither(second:))

Produces content for a conditional statement in a multi-statement closure
when the condition is false.

[`static   buildIf ( _ :)`](/documentation/SwiftUI/ViewBuilder/buildIf(_:))

Produces optional content for conditional statements in multi-statement
closures that’s only included when the condition evaluates to true.

[`static   buildLimitedAvailability ( _ :)`](/documentation/SwiftUI/ViewBuilder/buildLimitedAvailability(_:))



---

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)