Created
September 26, 2024 14:57
-
-
Save lucianoschillagi/b60ba2a50f8891706dfc1d64f0de04a6 to your computer and use it in GitHub Desktop.
SwiftUI Scene Phases
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import SwiftUI | |
| @main | |
| struct MyApp: App { | |
| @Environment(\.scenePhase) var scenePhase | |
| var body: some Scene { | |
| WindowGroup { | |
| ContentView() | |
| .onChange(of: scenePhase) { phase in | |
| switch phase { | |
| case .active: | |
| print("Now the scene is Active") | |
| // Active: The scene is in the foreground and interactive. | |
| case .inactive: | |
| print("Now the scene is Inactive") | |
| // Inactive: The scene is in the foreground and interactive. | |
| case .background: | |
| print("Now the scene is in Background") | |
| // Background: The scene isn’t currently visible in the UI. | |
| // Perform cleanup when all scenes within | |
| // MyApp go to the background. | |
| @unknown default: | |
| break | |
| } | |
| } | |
| } | |
| } | |
| } | |
| struct ContentView: View { | |
| var body: some View { | |
| VStack { | |
| Image(systemName: "moon") | |
| .imageScale(.large) | |
| .foregroundColor(.red) | |
| .padding(.bottom) | |
| Text("Hello, Moon!") | |
| .font(.title) | |
| } | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment