How it works
SelectionShapeStyle is the shape style that paints content with the system's current selection appearance — the same tint SwiftUI uses to highlight a selected row, cell, or control. Rather than hardcoding a specific accent color, you reach for it when you want a fill or stroke to read as "this is selected," and have it automatically match the platform, the user's accent-color preference, and the surrounding context. You almost never construct it directly; you use the .selection shorthand wherever a ShapeStyle is expected.
Reference the style through the
.selectionshorthandLike the other semantic shape styles,
SelectionShapeStyleis exposed as a static member so you can write it inline wherever aShapeStyleis accepted. In the example the style is named simply as.selection, which resolves to aSelectionShapeStylevalue — no initializer call and no color literal required.Fill a shape with it using
background(_:in:)Passing the style to
background(.selection, in:)fills the supplied shape with the selection tint and places it behind the view's content. HereRoundedRectangle(cornerRadius: 12)is the shape being filled, so theHStackofImageandTextsits on top of a selection-colored, rounded backdrop.Pair it with a contrasting
foregroundStyleBecause the selection tint is typically a saturated, system-driven color, the content drawn over it needs a deliberate foreground. The example sets
foregroundStyle(.white)so thecheckmark.circle.fillglyph and theTextstay legible against whatever color.selectionresolves to at runtime.Use it anywhere a
ShapeStyleis expectedSelectionShapeStyleconforms toShapeStyle, so it is interchangeable with styles like.tintor aColor. You can hand it toforegroundStyle,fill, or aShape's stroke just as readily as thebackground(_:in:)call shown here — the style adapts its concrete color to the context it is applied in.
.background(.selection, in: RoundedRectangle(cornerRadius: 12)) to .background(.tint, in: RoundedRectangle(cornerRadius: 12)) and compare the fill — the selection style tracks the system's selection highlight, which can differ from the plain accent tint.Example & preview
Press Run live & edit to compile it in your browser — then edit the Swift on the left and the preview re-renders live.
struct SelectionShapeStyleDemo: View {
var body: some View {
HStack(spacing: 8) {
Image(systemName: "checkmark.circle.fill")
Text("Selected Item")
}
.font(.headline)
.foregroundStyle(.white)
.padding()
.background(.selection, in: RoundedRectangle(cornerRadius: 12))
.padding()
}
}