Contents

containerBackground(for:alignment:content:)

Sets the container background of the enclosing container using a view.

Declaration

nonisolated func containerBackground<V>(for container: ContainerBackgroundPlacement, alignment: Alignment = .center, @ViewBuilder content: () -> V) -> some View where V : View

Parameters

  • container:

    The container that will use the background.

  • alignment:

    The alignment that the modifier uses to position the implicit Zstack that groups the background views. The default is Center.

  • content:

    The view to use as the background of the container.

Discussion

The following example uses a custom View as a background:

struct ContentView: View {
    var body: some View {
        NavigationStack {
            List {
                NavigationLink("Image") {
                    Text("Image")
                    .containerBackground(for: .navigation) {
                        Image(name: "ImageAsset")
                    }
                }
            }
        }
    }
}

The .containerBackground(for:alignment:content:) modifier differs from the background(_:ignoresSafeAreaEdges:) modifier by automatically filling an entire parent container. ContainerBackgroundPlacement describes the available containers.

See Also

Layering views