在 SwiftUI 中手动设置亮/暗模式并保存用户选择 [英] Manually set light/dark mode in SwiftUI and save users choice

查看:37
本文介绍了在 SwiftUI 中手动设置亮/暗模式并保存用户选择的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在此处找到的另一个线程中找到了在 swiftui 应用程序中手动设置亮/暗模式的解决方案 https://stackoverflow.com/a/58476468/11698443 基本有效,但有两个问题.

I found a solution to manually set light/dark mode in a swiftui app in another thread found here https://stackoverflow.com/a/58476468/11698443 it mostly works, but there are two problems.

  1. 用户选择不会永久保存.

  1. The users choice is not permanently saved.

我希望默认选择为暗模式,因此无论用户将系统设置为亮模式还是暗模式,应用最初都会以暗模式显示.

I would like the default choice to be dark mode, so the app will initially show up in dark mode whether the user has the system set to light or dark mode.

现在,这个实现有点小问题,因为如果用户在浅色模式下打开应用程序并按下切换开关.他们第一次按下开关时什么也不做.他们将不得不再按两次开关来触发 didSet 以使应用程序进入黑暗模式,即使如此,选择也不会被保存.

Right now, this implementation is a little buggy because if the user opens the app in light mode and hits the toggle switch. The first time they hit the switch will do nothing. They will have to hit the switch two more times to fire the didSet to get the app into dark mode and even then, the choice won't be saved.

其他一些线程询问暗模式实现,但大多数处理 UIKit 并且我上面链接的线程是我可以在 swiftui 中主要工作的唯一解决方案.是否可以修改该解决方案以解决我提出的两个问题?

A few other threads ask about dark mode implementation, but most deal with UIKit and the thread I linked to above was the only solution I could get to mostly work in swiftui. Is it possible to modify that solution to address the two issues that I brought up?

推荐答案

这是可能的方法(草率,你可以在这里找到默认值的 SO 属性包装器并使用它来获得更好的样式,但实现目标的想法是一样)

Here is possible approach (scratchy, you can find here on SO property wrapper for defaults and use it for better styling, but the idea to achieve your goal is the same)

使用 Xcode 11.4/iOS 13.4 测试

Tested with Xcode 11.4 / iOS 13.4

class SceneDelegate: UIResponder, UIWindowSceneDelegate {

    var window: UIWindow?

    private(set) static var shared: SceneDelegate?

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

        let contentView = ContentView()

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

            // restore from defaults initial or previously stored style
            let style = UserDefaults.standard.integer(forKey: "LastStyle")
            window.overrideUserInterfaceStyle = (style == 0 ? .dark : UIUserInterfaceStyle(rawValue: style)!)

            window.rootViewController = UIHostingController(rootView: contentView)
            self.window = window
            window.makeKeyAndVisible()
        }
    }

   ...
}


struct ContentView: View {
    var body: some View {
         // manipulate with style directly in defaults
         Toggle(isOn: Binding<Bool>(
            get: { UserDefaults.standard.integer(forKey: "LastStyle") !=
                        UIUserInterfaceStyle.light.rawValue },
            set: {
                SceneDelegate.shared?.window!.overrideUserInterfaceStyle = $0 ? .dark : .light
                UserDefaults.standard.setValue($0 ? UIUserInterfaceStyle.dark.rawValue : UIUserInterfaceStyle.light.rawValue, forKey: "LastStyle")
            }
         )) {
             Text("is Dark")
        }
    }
}

这篇关于在 SwiftUI 中手动设置亮/暗模式并保存用户选择的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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