WindowSceneReader

3.0.0

📱 Access UIWindowScene in SwiftUI
divadretlaw/WindowSceneReader

What's New

3.0.0

2024-02-28T09:41:27Z
  • Support visionOS

Full Changelog: 2.2.0...3.0.0

WindowSceneReader

Access the current UIWindowScene and UIWindow from any SwiftUI view.

Usage

SwiftUI Lifecycle

Read the current UIWindowScene with WindowSceneReader

@main
struct MyApp: App {
    var body: some Scene {
        WindowGroup {
            WindowSceneReader { windowScene in
                ContentView()
            }
        }
    }
}

alternatively, just add windowScene() if you only need the window scene on child views.

@main
struct MyApp: App {
    var body: some Scene {
        WindowGroup {
            ContentView()
                .windowScene()
        }
    }
}

On child views the UIWindowScene will be available in the Environment

UIKit Lifecycle

class SceneDelegate: UIResponder, UIWindowSceneDelegate {
    var window: UIWindow?
    
    func scene(_ scene: UIScene, willConnectTo session: UISceneSession, options connectionOptions: UIScene.ConnectionOptions) {
        if let windowScene = scene as? UIWindowScene {
            let rootView = ContentView()
                .windowScene(windowScene)
            
            let window = UIWindow(windowScene: windowScene)
            window.rootViewController = UIHostingController(rootView: rootView)
            self.window = window
            window.makeKeyAndVisible()
    }
}

Environment

@Environment(\.windowScene) var windowScene

The @Environment(\.windowScene) var windowScene defaults to the first connected UIWindowScene or nil if no UIWindowScene is connected.

License

See LICENSE

Description

  • Swift Tools 5.9.0
View More Packages from this Author

Dependencies

Last updated: Fri Oct 18 2024 08:55:16 GMT-0900 (Hawaii-Aleutian Daylight Time)