selection
A style used to visually indicate selection following platform conventional colors and behaviors.
Declaration
static var selection: SelectionShapeStyle { get }Discussion
For example:
ForEach(items) {
ItemView(value: item, isSelected: item.id == selectedID)
}
struct ItemView {
var value: item
var isSelected: Bool
var body: some View {
// construct the actual cell content
.background(isSelected
? AnyShapeStyle(.selection)
: AnyShapeStyle(.fill.quaternary),
in: .rect(cornerRadius: 6))
}
}On macOS and iPadOS this automatically reflects window key state and focus state, where the emphasized appearance will be used only when the window is key and the nearest focusable element is actually focused. On iPhone, this will always fill with the environment’s accent color.
When applied as a background of another view, it will automatically set the EnvironmentValues.backgroundProminence for the environment of that view to match the current prominence of the selection.
For information about how to use shape styles, see ShapeStyle.