跳转至
Enumeration

ColorScheme

Indicates the type of color scheme, such as Dark Mode.

Declaration

enum ColorScheme : CaseIterable

Overview

The color scheme enumerates the user setting options for Light or Dark Mode. It also provides the light or dark options for any particular view when the app wants to override the user setting.

Color scheme has two cases:

These can be applied using either the colorScheme(_:) modifier, which automatically sets the ColorScheme for the View, or the preferredColorScheme(_:) modifier, which is overridden by declarations of preferredColorScheme(_:) higher up in the View hierarchy.

A light mode effect, with a light background and darker text, can be achieved using the .light case. For example:

struct ContentView: View {
    var body: some View {
        Text("Dark🖤 or Light🤍 Mode?")
            .preferredColorScheme(ColorScheme.light)
    }
}
A view containing a VStack with a textview reading "Dark🖤 or Light🤍 Mode?" and the colorscheme is set to light, making the background white and the text black.

A dark mode effect, with a dark background and lighter text, can be achieved using the .dark case. For example:

struct ContentView: View {
    var body: some View {
        Text("Dark🖤 or Light🤍 Mode?")
            .preferredColorScheme(ColorScheme.dark)
    }
}
A view containing a VStack with a textview reading "Dark🖤 or Light🤍 Mode?" and the colorscheme is set to dark, making the background black and the text white.

These ColorSchemes can also be used in colorScheme. Take this example, where different Text renders depending on the system's current color scheme setting:

struct ContentView: View {
    @Environment(\.colorScheme) var scheme

    var body: some View {
        Text(scheme == ColorScheme.light ? "Light 🤍" : "Dark 🖤")
    }
}

Availability

iOS 13.0+

macOS 10.15+

tvOS 13.0+

watchOS 6.0+

Topics


Type Alias

AllCases A type that can represent a collection of all values of this type.


Type Method

==(a:b:) Returns a Boolean value indicating whether two values are equal.


Initializer

init(_:) Create a color scheme from its UIUserInterfaceStyle equivalent.


Instance Method

hash(into:) Hashes the essential components of this value by feeding them into the given hasher.


Case

dark The color scheme referring to dark mode in a device.

light The color scheme referring to light mode in a device.


Instance Property

hashValue The hash value.


Type Property

allCases A collection of all values of this type.