9
2

More than 3 years have passed since last update.

[SwiftUI]プレビューをDark Modeにする

Posted at

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は通常;黒、ダークモード:白

プレビュー

通常モード

ダークモード

9
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
9
2