Structure

ViewBuilder

A custom parameter attribute that constructs views from closures.

Declaration

struct ViewBuilder

Overview

You typically use ViewBuilder as a parameter attribute for child 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 shown in the following example:

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

Topics

Building Blocks

static func buildBlock() -> EmptyView

Builds an empty view from a block containing no statements.

static func buildBlock<Content>(Content) -> Content

Passes a single view written as a child view through unmodified.

static func buildEither<TrueContent, FalseContent>(first: TrueContent) -> ConditionalContent<TrueContent, FalseContent>

Provides support for “if” statements in multi-statement closures, producing conditional content for the “then” branch.

static func buildEither<TrueContent, FalseContent>(second: FalseContent) -> ConditionalContent<TrueContent, FalseContent>

Provides support for “if-else” statements in multi-statement closures, producing conditional content for the “else” branch.

static func buildIf<Content>(Content?) -> Content?

Provides support for “if” statements in multi-statement closures, producing an optional view that is visible only when the condition evaluates to true.

See Also

Supporting Types

struct StaticMember

A concrete wrapper for enabling implicit member expressions.

Beta
protocol ViewModifier

A modifier that you apply to a view or another view modifier, producing a different version of the original value.

Beta
struct IdentifierValuePair

An identifier and its corresponding value.

Beta
struct IdentifierValuePairs

A collection of identifier-value pairs computed on demand.

Beta

Beta Software

This documentation contains preliminary information about an API or technology in development. This information is subject to change, and software implemented according to this documentation should be tested with final operating system software.

Learn more about using Apple's beta software