Ben Myers
Ben Myers

Reputation: 1395

How can I change window.rootViewController in Xcode 12's new SwiftUI App lifecycle?

I am new to SwiftUI. I have a new project, Test, running the SwiftUI interface and the SwiftUI App lifecycle. This lifecycle is new to iOS 14 and Xcode 12.

I'd like to use a Github package, BetterSheet, in my project. I've added the package dependency. However, as described in the Basic Usage section of the readme, I have to initialize UIHostingController with sheet support in SceneDelegate.swift, like so:

window.rootViewController = UIHostingController.withBetterSheetSupport(rootView: ContentView())

My app does not have SceneDelegate.swift due to the nature of the new lifecycle. I have TestApp.swift instead:

@main
struct TestApp: App {
    var body: some Scene {
        WindowGroup {
            ContentView()
        }
    }
}

I want to use the BetterSheet package in my project, but this first step seems to get in the way. How can I get around this?

Upvotes: 1

Views: 612

Answers (1)

Claus Jørgensen
Claus Jørgensen

Reputation: 26347

I'd suspect you can do something like this, based on their UIHostingController+BetterSheet.swift code.

@main
struct TestApp: App {
    let coordinator = BetterSheetCoordinator()
    var body: some Scene {
        WindowGroup {
            BetterSheetSupport(coordinator: coordinator) {
                ContentView()
            }
        }
    }
}

But I would question the value of this library. Sheet presentation in iOS 14 with the standard library works just fine, and the library itself haven't been updated for 14 months.

Upvotes: 3

Related Questions