SwiftUI Previews Light/Dark Mode

Preview light and dark modes

Set a preferred color scheme in previews to spot contrast issues and verify colors in both modes.

Example

DarkMode.swift

import SwiftUI

struct DemoView: View { var body: some View { Text("Hello") } }

#Preview("Light") {
  DemoView()
.preferredColorScheme(.light)
} #Preview("Dark") { DemoView()
.preferredColorScheme(.dark)
}

In the example above, the .preferredColorScheme(.dark) modifier is used to set the preferred color scheme to dark.

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *