Overloads
7 ways to call compose().background(). Each is one SwiftUI overload, picked by the arguments you pass.
compose().background(
style: SwiftUI.ShapeStyle,
options?: {
ignoresSafeAreaEdges?: SwiftUI.EdgeSet;
}
)nonisolated func background<S>(
_ style: S,
ignoresSafeAreaEdges edges: Edge.Set = .all
) -> some View
where S : ShapeStyleParameters
style*SwiftUI.ShapeStyleAn instance of a type that conforms to
ShapeStylethat SwiftUI draws behind the modified view.options.ignoresSafeAreaEdgesSwiftUI.EdgeSet
Return value
A view with the specified style drawn behind it.
Discussion
Use this modifier to place a type that conforms to the ShapeStyle
protocol --- like a Color, Material, or
HierarchicalShapeStyle --- behind a view. For example, you can add
the ShapeStyle.regularMaterial behind a Label:
struct FlagLabel: View {
var body: some View {
Label("Flag", systemImage: "flag.fill")
.padding()
.background(.regularMaterial)
}
}SwiftUI anchors the style to the view's bounds. For the example above, the background fills the entirety of the label's frame, which includes the padding:
SwiftUI limits the background style's extent to the modified view's
container-relative shape. You can see this effect if you constrain the
FlagLabel view with a View.containerShape(_:) modifier:
FlagLabel()
.containerShape(RoundedRectangle(cornerRadius: 16))The background takes on the specified container shape:
By default, the background ignores safe area insets on all edges, but you can provide a specific set of edges to ignore, or an empty set to respect safe area insets on all edges:
Rectangle()
.background(
.regularMaterial,
ignoresSafeAreaEdges: []) // Ignore no safe area insets.If you want to specify a View or a stack of views as the background,
use View.background(alignment:content:) instead.
To specify a Shape or InsettableShape, use
View.background(_:in:fillStyle:) .
To configure the background of a presentation, like a sheet, use
View.presentationBackground(_:).
See also
More in Appearance.
- badgeGenerates a badge for the view from a localized string resource.
- badgeProminenceSpecifies the prominence of badges created by this view.
- borderAdds a border to this view with the specified style and width.
- buttonSizingThe preferred sizing behavior of buttons in the view hierarchy.
- containerBackgroundSets the container background of the enclosing container using a view.
- headerProminenceSets the header prominence for this view.
- hiddenHides this view unconditionally.
- imageScaleScales images within the view according to one of the relative sizes available including small, medium, and large images sizes.
- labelIconToTitleSpacingSet the spacing between the icon and title in labels.
- labelReservedIconWidthSet the width reserved for icons in labels.
- labelsHiddenHides the labels of any controls contained within this view.
- labelsVisibilityControls the visibility of labels of any controls contained within this view.