Hide the Home Indicator in Swift 2.0

Viewed 823

I'm looking to hide the Home Indicator and while this is straightforward in Swift doesn't appear to be as easy in SwiftUI.

I attempted to use this:

How to hide the home indicator with SwiftUI?

But with the removal of the SceneDelegate I'm too green to know how to properly translate that for the new app protocol.

Anyone have any thoughts?

Dan

1 Answers

Here is possible approach to replace default WindowGroup window's hosting controller with any custom one (in this case w/o home indicator).

The helper extension are taken from before provided solution in https://stackoverflow.com/a/63276688/12299030.

Tested with Xcode 12 / iOS 14

@main
struct MyApp: App {
    @UIApplicationDelegateAdaptor(MyAppDelegate.self) var appDelegate

    var body: some Scene {
        WindowGroup {
            Text("")  // << temporary placeholder
                .withHostingWindow { window in
                    let contentView = ContentView().environmentObject(SomeObservableObject())
                    window?.rootViewController = 
                         HideHomeIndicatorController(rootView: contentView)
                }
        }
    }
}

and simplified variant of hosting controller to hide home indicator

class HideHomeIndicatorController<Content:View>: UIHostingController<Content> {
    override var prefersHomeIndicatorAutoHidden: Bool {
        true
    }
}
Related