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
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
Adding a background to your viewZStackzIndex(_:)background(alignment:content:)background(_:ignoresSafeAreaEdges:)background(ignoresSafeAreaEdges:)background(_:in:fillStyle:)background(in:fillStyle:)overlay(alignment:content:)overlay(_:ignoresSafeAreaEdges:)overlay(_:in:fillStyle:)backgroundMaterialcontainerBackground(_:for:)ContainerBackgroundPlacement