Store the window scene in an environment object when creating the view hierarchy. The environment object can be an existing object that you use for other app-wide data.
final class AppData: ObservableObject {
let windowScene: UIWindow? // Will be nil in SwiftUI previewers
init(windowScene: UIWindowScene? = nil) {
self.windowScene = windowScene
}
}
Set the window scene when you create the environment object. Add the object to the view at the base of your view hierarchy, such as the root view.
let rootView = RootView().environmentObject(AppData(windowScene: windowScene))
Finally, use the window scene to access the status bar height.
struct MyView: View {
@EnvironmentObject private var appData: AppData
...
var body: some View {
SomeView().frame(height: self.appData.windowScene?.statusBarManager?.statusBarFrame.height ?? 0)
}
}