4

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

lezone
  • 41
  • 3

1 Answers1

1

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
    }
}
Asperi
  • 228,894
  • 20
  • 464
  • 690
  • Looks like this is doing something similar to this: https://stackoverflow.com/a/64623130/14348570 And I'm running into the same issue with both. I'm totally green but it seems that in both cases it is replacing ContentView()? I have several .environmentObject() initialized under a WindowGroup in the @main App that I think are therefore being skipped as a result? (Sorry if that isn't quite what's happening but hopefully is the general gist.) – lezone Nov 01 '20 at 03:57
  • Read my comment to that post about pitfall. This solution is different - it only replaces root view controller in created window. See updated on how/where to inject environment object in this case. – Asperi Nov 01 '20 at 05:46
  • Okay, I got that working for all views, is there a way to limit it to only affecting when in landscape? – lezone Nov 14 '20 at 21:18