我试图改变应用的主题与三个选项:默认,黑暗,光明。然后,视图跳转到主视图,但我希望停留在设置视图上。

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])
}
}
...在"MyApp.swift":
@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()
}
}
}
}发布于 2020-12-18 10:09:55
为什么你要让应用程序负责黑暗和光明的主题,我认为你应该让系统处理这一点。然后检查你的功能。如果您调用ContentView,则显示它是有意义的。
如果你不想让系统处理这件事。查看这篇文章,How to switch programmatically to dark mode swift,也许这对你有帮助
https://stackoverflow.com/questions/65354877
复制相似问题