title: How to change tintColor of whole app? SwiftUI
body: <p>So, I am supporting three themes in my app, each with different tintColors. I'm using @EnvironmetObject to track changes. However, I can't use it on SceneDelegate.swift file, because the app crashes. Moreover, accentColor is not an option, as it doesn't change alert tintColor. How can I do it?</p>
  
  <p>Here's some code:</p>
  
  <blockquote>
    <p>SceneDelegate.swift file</p>
  </blockquote>
  
  <pre><code>class SceneDelegate: UIResponder, UIWindowSceneDelegate {
  
  @EnvironmentObject var userData: UserData
  
  var window: UIWindow?
  
  func scene(_ scene: UIScene, willConnectTo session: UISceneSession, options connectionOptions: UIScene.ConnectionOptions) {
      // Use this method to optionally configure and attach the UIWindow `window` to the provided UIWindowScene `scene`.
      // If using a storyboard, the `window` property will automatically be initialized and attached to the scene.
      // This delegate does not imply the connecting scene or session are new (see `application:configurationForConnectingSceneSession` instead).
  
      // Create the SwiftUI view that provides the window contents.
      let contentView = TasksView()
  
      // Use a UIHostingController as window root view controller.
      if let windowScene = scene as? UIWindowScene {
          let window = UIWindow(windowScene: windowScene)
          window.rootViewController = UIHostingController(rootView: contentView.environmentObject(UserData()))
          self.window = window
          window.makeKeyAndVisible()
  
          window.tintColor = userData.selectedTheme.tintColor
      }
  }
  </code></pre>
  
  <p>This approach will crash when the app starts, because it can't finde an @EnvironmentObject in its ancestor.</p>
  
  <blockquote>
    <p>ContentView.swift file</p>
  </blockquote>
  
  <pre><code>struct ContentView: View {
  
      @EnvironmentObject var userData: UserData
  
      var body: some View {
          NavigationView{
              List(userData.tasks) { task in
                  TaskRow(taskTitle: task.title, taskDetail: task.detail)
              }
              .navigationBarTitle(Text("Tasks"), displayMode: .automatic)
  
              .navigationBarItems(
                  leading: NavigationLink(destination: SettingsView(), label: {
                      Image(systemName: "gear").imageScale(.large)
                  }),
                  trailing: NavigationLink(destination: AddTaskView(), label: {
                      Image(systemName: "plus").imageScale(.large)
                  })
              )
          }.navigationViewStyle(StackNavigationViewStyle())
           .accentColor(userData.selectedTheme.accentColor)
      }
  }
  </code></pre>
  
  <p>This approach won't work for me either because it doesn't change the tintColor of alerts, for example.</p>
  
  <blockquote>
    <p>Images</p>
  </blockquote>
  
  <p><a href="https://i.stack.imgur.com/MYxAT.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/MYxAT.png" alt="This is what I get if I use accentColor"></a></p>
  
  <p>This is what I get if I use accentColor</p>
  
  <hr>
  
  <p><a href="https://i.stack.imgur.com/prbBo.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/prbBo.png" alt="This is what I want to achieve"></a></p>
  
  <p>This is what I want to achieve</p>