SwiftUI 三元集主题在重新打开视图之前不会更新

问题描述 投票:0回答:1

我的应用程序有自定义配色方案,但我也使用一些基本的内置视图。一个例子是我有一张表来控制应用程序的设置。使用选择器更改应用程序的主题,然后根据选择的主题更新

.preferredColorScheme
.preferredColorScheme(theme.foregroundColor == .dark ? .light : .dark)

但是,在关闭并重新打开之前,纸张的颜色不会改变。有没有办法强制更新工作表的颜色主题?

示例:

@Binding var theme: Theme
/* ... */
.sheet(isPresented: true) {
    NavigationStack {
        Form {
            Picker("Theme", selection: $theme) {
                ForEach(Theme.allCases) { th in
                    Text(th.name).tag(th)
                }
            }
            .pickerStyle(.navigationLink)
        }
    }
}
.preferredColorScheme(theme.accentColor == .dark ? .light : .dark)
swiftui
1个回答
0
投票

要同时更改工作表主题,请在工作表视图中添加另一个

.preferredColorScheme(theme.accentColor == .dark ? .light : .dark)
,例如:

 .sheet(isPresented: true) {
     NavigationStack {
         Form {
             Picker("Theme", selection: $theme) {
                 ForEach(Theme.allCases) { th in
                     Text(th.name).tag(th)
                 }
             }
             .pickerStyle(.navigationLink)
         }
     }
    .preferredColorScheme(theme.accentColor == .dark ? .light : .dark) // <-- here
 }
 .preferredColorScheme(theme.accentColor == .dark ? .light : .dark)
© www.soinside.com 2019 - 2024. All rights reserved.