Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/116.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 使用@AppStorage更改SwiftUI暗/亮模式_Ios_Swift_Xcode_Swiftui_Userdefaults - Fatal编程技术网

Ios 使用@AppStorage更改SwiftUI暗/亮模式

Ios 使用@AppStorage更改SwiftUI暗/亮模式,ios,swift,xcode,swiftui,userdefaults,Ios,Swift,Xcode,Swiftui,Userdefaults,我正在尝试用3个选项更改应用程序的主题:默认、暗、亮。之后,视图跳转到主视图,但我想保留设置视图 为什么你要让应用程序负责黑暗和光明的主题,我认为你应该让系统来处理这个问题。然后检查你的功能。如果调用ContentView,则显示它是有意义的 如果你不想让系统处理这个问题。查看这篇文章也许这对你有帮助为什么你要让应用程序负责黑暗和光明主题,我认为你应该让系统来处理这个问题。然后检查你的功能。如果调用ContentView,则显示它是有意义的 如果你不想让系统处理这个问题。查看此帖子可能有助于您

我正在尝试用3个选项更改应用程序的主题:默认、暗、亮。之后,视图跳转到主视图,但我想保留设置视图


为什么你要让应用程序负责黑暗和光明的主题,我认为你应该让系统来处理这个问题。然后检查你的功能。如果调用ContentView,则显示它是有意义的


如果你不想让系统处理这个问题。查看这篇文章也许这对你有帮助

为什么你要让应用程序负责黑暗和光明主题,我认为你应该让系统来处理这个问题。然后检查你的功能。如果调用ContentView,则显示它是有意义的


如果你不想让系统处理这个问题。查看此帖子可能有助于您

您需要一些应用程序状态,以便在ContentView中返回设置视图。您需要一些应用程序状态,以便在ContentView中返回设置视图。
struct SettingsView: View {
...
@State var colors = ["Alapértelmezett", "Sötét", "Világos"] //Default, Dark, Light
@AppStorage("colorIndex") var colorIndex: Int = 0
...
var body: some View {
...
Picker(selection: $colorIndex, label: Text("Megjelenés")) { //Color
                            ForEach(0 ..< colors.count) {
                                Text(self.[$0])
                            }
                            
                        }

...
@main
struct MyApp: App {

        
    @AppStorage("colorIndex") var colorIndex: Int = 0
    
    @UIApplicationDelegateAdaptor(AppDelegate.self) var appDelegate
    var body: some Scene {
        WindowGroup {

            if colorIndex == 1 {
                
                ContentView().environment(\.colorScheme, .dark)
            }
            else if colorIndex == 2 {
                ContentView().environment(\.colorScheme, .light)
            }
            else {
                ContentView()
            }
                

        }
        
    }

}