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

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

问题描述

因此,我在我的应用中支持三个主题,每个主题都有不同的 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.

图片

这就是我使用重音颜色时得到的

This is what I get if I use accentColor

这就是我想要的

推荐答案

在您的 Assets.xcassets 文件中应该是 AccentColor 的条目.单击此条目并选择 Universal 矩形.之后,您可以在右侧的属性检查器中设置您想要的任何颜色.

In your Assets.xcassets file should be an entry for AccentColor. Click on this entry and select the Universal rectangle. Afterwards you can set any color you want in the attribute inspector on the right hand side.

这将为您的所有视图设置颜色,甚至适用于模态视图,如果您尝试手动覆盖它们,它们的行为会有点奇怪.

This will set the color for all your views and applies even to modal views, which behave kind of weird if you try to override them manually.

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

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