跳转至
Structure

AnyView

A view that provides type-erasure for views of other types.

Declaration

@frozen struct AnyView : View

Overview

An AnyView hides the type of the View value passed to it (similar to how AnyHashable hides the type of Hashable value passed to it).

To type-erase your view, pass it to init(_:) like this:

struct ExampleView: View {
    var body: some View {
        AnyView(Text("Hello, world!"))
    }
}
A white AnyView displaying the text "Hello,world!"

Changing the type of view using with AnyView destroys the view hierarchy for the old type and creates a new hierarchy for the new type. In the following example, Text is destroyed and Color is created when foo is set to false:

struct ExampleView: View {
    let foo: Bool = false

    var body: some View {
        if foo {
            AnyView(Text("Hello, world!"))
        } else {
            AnyView(Color.red)
        }
    }
}
A blank red AnyView.

Avoid frequently changing the underlying type of view being erased, especially for complex views, as poor performance may result. AnyView is best suited for use in the deepest part of your view hierarchy, such as a list's row content in init(_:id:selection:rowContent:). It is also suited for use in different layers of your view hierarchy, via either overlay(_:alignment:) or background(_:alignment:).

Consider whether the usage of AnyView is appropriate, or even necessary. A common mistake is to use AnyView like this:

func makeHelperView() -> some View {
    if foo {
        return AnyView(Text("Hello, world!"))
    } else {
        return AnyView(Color.red)
    }
}
Wherein adding a ViewBuilder would be more appropriate:

@ViewBuilder
func makeHelperView() -> some View {
    if foo {
        Text("Hello, world!")
    } else {
        Color.red
    }
}
A blank red AnyView.

The latter example performs better as the SwiftUI runtime is given a more explicit type hierarchy, where the switch between foo being true or false can only result in a Text or a Color. The former example erases that information, forcing the SwiftUI runtime to evaluate the actual view type lazily, and thus requires more work to compute and render.

Availability

iOS 13.0+

macOS 10.15+

tvOS 13.0+

watchOS 6.0+

Topics


Type Alias

Body The type of view representing the body of this view.


Initializer

init(_:) Create an instance that type-erases View.

init(erasing:) Creates an instance that type-erases the parameter.