Post

Replies

Boosts

Views

Activity

Reply to SwiftUI: Hiding the status bar with TabView and nested NavigationView's
I found the only way to get this to work per view is to wrap your rootView in a HostingController like: class HostingController<ContentView>: UIHostingController<ContentView> where ContentView : View {          init(view: ContentView) {         super.init(rootView: view)         NotificationCenter.default.addObserver(forName: .onDarkStatusBar, object: nil, queue: .main) { _ in             self.statusBarEnterDarkBackground()         }                  NotificationCenter.default.addObserver(forName: .onLightStatusBar, object: nil, queue: .main) { _ in             self.statusBarEnterLightBackground()         }                  NotificationCenter.default.addObserver(forName: .onShowStatusBar, object: nil, queue: .main) { _ in             self.statusBarShow()         }                  NotificationCenter.default.addObserver(forName: .onHideStatusBar, object: nil, queue: .main) { _ in             self.statusBarHide()         }     }          @objc required dynamic init?(coder aDecoder: NSCoder) {         fatalError("init(coder:) has not been implemented")     }          private var isDarkContentBackground = false     private var isStatusBarHiden = false     func statusBarEnterDarkBackground() {         isDarkContentBackground = false         setNeedsStatusBarAppearanceUpdate()     }     func statusBarEnterLightBackground() {         isDarkContentBackground = true         setNeedsStatusBarAppearanceUpdate()     }          func statusBarHide() {         isStatusBarHiden = true         setNeedsStatusBarAppearanceUpdate()     }     func statusBarShow() {         isStatusBarHiden = false         setNeedsStatusBarAppearanceUpdate()     }     override var preferredStatusBarStyle: UIStatusBarStyle {         if isDarkContentBackground {             return .lightContent         }         else         {             return .darkContent         }     }          override var prefersStatusBarHidden: Bool {       return isStatusBarHiden     } } Use as let window = UIWindow(windowScene: windowScene)             window.rootViewController = HostingController(view: contentView) Then send out a notification from onDissapear and onAppear. extension Notification.Name {   static let onDarkStatusBar = Notification.Name("onDarkStatusBar")     static let onLightStatusBar = Notification.Name("onLightStatusBar")     static let onHideStatusBar = Notification.Name("onHideStatusBar")     static let onShowStatusBar = Notification.Name("onShowStatusBar") } Using                             .onAppear {                                 NotificationCenter.default.post(name: Notification.Name.onHideStatusBar, object: nil)                             }                             .onDisappear {                                 NotificationCenter.default.post(name: Notification.Name.onShowStatusBar, object: nil)                             }
Aug ’21