我的应用程序有自定义配色方案,但我也使用一些基本的内置视图。一个例子是我有一张表来控制应用程序的设置。使用选择器更改应用程序的主题,然后根据选择的主题更新
.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)
要同时更改工作表主题,请在工作表视图中添加另一个
.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)