如何更改整个应用程序的tintColor? SwiftUI [英] How to change tintColor of whole app? SwiftUI

查看:243
本文介绍了如何更改整个应用程序的tintColor? SwiftUI的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

因此,我在我的应用程序中支持三个主题,每个主题具有不同的tintColors.我正在使用@EnvironmetObject跟踪更改.但是,我无法在SceneDelegate.swift文件上使用它,因为该应用程序崩溃了.而且,accentColor不是一个选项,因为它不会更改警报tintColor.我该怎么办?

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?

以下是一些代码:

SceneDelegate.swift文件

SceneDelegate.swift file

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
    }
}

这种方法在应用启动时会崩溃,因为它无法在其祖先中找到@EnvironmentObject.

This approach will crash when the app starts, because it can't finde an @EnvironmentObject in its ancestor.

ContentView.swift文件

ContentView.swift file

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)
    }
}

例如,这种方法对我也不起作用,因为它不会更改警报的tintColor.

This approach won't work for me either because it doesn't change the tintColor of alerts, for example.

图片

这是我使用accentColor会得到的

This is what I get if I use accentColor

这是我想要实现的

推荐答案

更新:此演示已在GitHub上发布-

Update: posted this demo on GitHub - DemoWindowTint

以下演示是使用在演示中,我使用了NavigationView和几个NavigationLinksButton来显示Alert.

In demo I used NavigationView with couple of NavigationLinks and Button showing Alert.

经过以下测试

class SceneDelegate: UIResponder, UIWindowSceneDelegate {

    var window: UIWindow?


    func scene(_ scene: UIScene, willConnectTo session: UISceneSession, options connectionOptions: UIScene.ConnectionOptions) {

        if let windowScene = scene as? UIWindowScene {
            let window = UIWindow(windowScene: windowScene)

            let contentView = ContentView()
                .environment(\.hostingWindow, { [weak window] in
                    return window })

            window.rootViewController = UIHostingController(rootView: contentView)

            self.window = window
    ...

struct ContentView: View {
    @Environment(\.hostingWindow) var hostingWindow
   ... // body can be any

    .onAppear {
            // can be loaded from UserDefaults here, and later changed on any action
            self.hostingWindow()?.tintColor = UIColor.red
    }

这篇关于如何更改整个应用程序的tintColor? SwiftUI的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆