Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/divadretlaw/windowscenereader
📱 Access UIWindowScene in SwiftUI
https://github.com/divadretlaw/windowscenereader
swift swiftui uiwindow uiwindowscenedelegate
Last synced: 2 months ago
JSON representation
📱 Access UIWindowScene in SwiftUI
- Host: GitHub
- URL: https://github.com/divadretlaw/windowscenereader
- Owner: divadretlaw
- License: mit
- Created: 2022-08-12T07:55:56.000Z (over 2 years ago)
- Default Branch: main
- Last Pushed: 2024-09-03T17:21:29.000Z (4 months ago)
- Last Synced: 2024-09-19T18:34:54.201Z (4 months ago)
- Topics: swift, swiftui, uiwindow, uiwindowscenedelegate
- Language: Swift
- Homepage:
- Size: 34.2 KB
- Stars: 5
- Watchers: 3
- Forks: 2
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# WindowSceneReader
[![](https://img.shields.io/endpoint?url=https%3A%2F%2Fswiftpackageindex.com%2Fapi%2Fpackages%2Fdivadretlaw%2FWindowSceneReader%2Fbadge%3Ftype%3Dplatforms)](https://swiftpackageindex.com/divadretlaw/WindowSceneReader)
[![](https://img.shields.io/endpoint?url=https%3A%2F%2Fswiftpackageindex.com%2Fapi%2Fpackages%2Fdivadretlaw%2FWindowSceneReader%2Fbadge%3Ftype%3Dswift-versions)](https://swiftpackageindex.com/divadretlaw/WindowSceneReader)
Access the current `UIWindowScene` and `UIWindow` from any SwiftUI view.
## Usage
### SwiftUI Lifecycle
Read the current `UIWindowScene` with `WindowSceneReader`
```swift
@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.
```swift
@main
struct MyApp: App {
var body: some Scene {
WindowGroup {
ContentView()
.windowScene()
}
}
}
```On child views the `UIWindowScene` will be available in the `Environment`
### UIKit Lifecycle
```swift
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
```swift
@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](LICENSE)