dark mode default swiftui 1 code example

Example 1: dark mode change immediately swift

guard let appDelegate = UIApplication.shared.delegate as? AppDelegate else {
   return
}
appDelegate.changeTheme(themeVal)


// App Delegate Change Theme method
func changeTheme(themeVal: String) {  
  if #available(iOS 13.0, *) {
     switch themeVal {
     case "dark":
         window?.overrideUserInterfaceStyle = .dark
         break
     case "light":
         window?.overrideUserInterfaceStyle = .light
         break
     default:
         window?.overrideUserInterfaceStyle = .unspecified
     }
  }
}

Example 2: make preivew in dark mode swiftui

struct ContentView_Previews: PreviewProvider {
    static var previews: some View {
        ContentView().preferredColorScheme(.dark)
    }
}