Type Property 型プロパティ

selection

A style used to visually indicate selection following platform conventional colors and behaviors.

Declaration 宣言

static var selection: SelectionShapeStyle { get }
Available when Self is SelectionShapeStyle.

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(selectionBackground)
    }
    @ViewBuilder
    private var selectionBackground: some View {
        if isSelected {
            RoundedRectangle(cornerRadius: 8)
                .fill(.selection)
        }
    }
}

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.

For information about how to use shape styles, see ShapeStyle.

See Also 参照

Semantic Styles