Post

Replies

Boosts

Views

Activity

Reply to View presented as sheet are properly updated to correct color scheme
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:
Sep ’25
Reply to SwiftUI onAppear called inconsistently based on the presence of listStyle
I'm aware this is an old topic but I am also facing this situation today (2024). That's too bad that the .onAppear() does not behave as the name implies. I understand that the behaviours between SwiftUI and UIKit are not quite the same (UIKit version viewDidAppear / viewWillAppear will always be called when a view will be facing to the user, that's because UIKit manipulates class instances VS SwiftUI which uses structs, so the view hierarchy will not be changed in some cases), but I think the name .onAppear() should be changed to something more descriptive (e.g. onViewAttached or onViewHierarchyChanged)
Topic: UI Frameworks SubTopic: SwiftUI Tags:
Nov ’24