Instance Method

preferredColorScheme(_:)

Sets the preferred color scheme for this presentation.

Declaration

func preferredColorScheme(_ colorScheme: ColorScheme?) -> some View

Parameters

colorScheme

The color scheme for this view.

Return Value

A view that wraps this view and sets the color scheme.

Discussion

The color scheme applies to the nearest enclosing presentation, such as a popover or window. Views may read the color scheme using the colorScheme environment value.

In the example below the presentation containing the VStack has its color scheme set to ColorScheme.dark:

VStack {
    Button(action: {}) {
        Text(" Button")
    }
    HStack {
        Text(" Slider").accentColor(Color.green)
        Slider(value: $sliderValue, in: -100...100, step: 0.1)
    }
}.preferredColorScheme(.dark)

See Also

Adopting View Color Schemes

func colorScheme(ColorScheme) -> View

Sets the view’s color scheme.

enum ColorScheme

The possible types of color schemes, like Dark Mode.

struct PreferredColorSchemeKey

A key for specifying the preferred color scheme.