Someone (Andy) showed me a "workaround" (i guess?) by using the @Environment(\.colorScheme) to update the proper color scheme for sheets (see the updated ContentView)
BTW I still think this is a SwiftUI issue. Sheets should behave the same way other views are presented.
Here is a working version of the sample:
import SwiftUI
enum AppTheme: Int {
case system = 0
case dark = 1
case light = 2
var colorScheme: ColorScheme? {
switch self {
case .system: return nil
case .light: return .light
case .dark: return .dark
}
}
}
struct SettingsView: View {
@AppStorage("theme") var appTheme: AppTheme = .system
var body: some View {
VStack(spacing: 8) {
Button {
select(theme: .system)
} label: {
Text("System")
}
Button {
select(theme: .dark)
} label: {
Text("Dark")
}
Button {
select(theme: .light)
} label: {
Text("Light")
}
}
}
func select(theme: AppTheme) {
appTheme = theme
}
}
struct ContentView: View {
@AppStorage("theme") var appTheme: AppTheme = .system
@State var isPresented = false
@Environment(\.colorScheme) var systemColorScheme
private var effectiveColorScheme: ColorScheme {
// For sheets, we can't pass nil to preferredColorScheme, so we resolve the actual system scheme
appTheme.colorScheme ?? systemColorScheme
}
var body: some View {
NavigationStack {
VStack {
Button {
isPresented = true
} label: {
Text("Present settings")
}
}
.preferredColorScheme(appTheme.colorScheme)
.sheet(isPresented: $isPresented) {
SettingsView()
.preferredColorScheme(effectiveColorScheme)
}
}
}
}
#Preview {
ContentView()
}
Topic:
UI Frameworks
SubTopic:
SwiftUI
Tags: