SwiftUIのプレビューでDark Modeを試したい時のメモです。
ソース
import SwiftUI
struct ContentView: View {
var body: some View {
ZStack {
Color.init("Background")
.edgesIgnoringSafeArea(.all)
Text("Hello, World!")
.foregroundColor(Color("TextPrimary"))
}
}
}
struct ContentView_Previews: PreviewProvider {
static var previews: some View {
ContentView()
.environment(\.colorScheme, .dark)
}
}
ポイントは.environment(\.colorScheme, .dark)
で指定しているところです。
通常は.light
でダークモードは.dark
を指定します。
切り替えることですぐに確認ができます。
ソースで使っている色については
- Backgroundは通常:白、ダークモード:黒
- Textは通常;黒、ダークモード:白