1

How can I change the color of the status bar when using a SplitViewController? When using just a viewcontroller with a navigationcontroller, the color of the status bar changes automatically by using ①. Are there any ways I can change the status bar automatically?

I want to add a dark mode to my app so, I can't change the status bar style from the project/target settings.

In my app I have a Notification that turns on and off my dark mode setting.

I have tried

self.navigationController?.navigationBar.barStyle = .black

②changing the info.plist

Status bar Light content doesnot appear in Navigation Controller using Split view controller

This is what it looks like and I'm using ①to change the color of the navigationbar

Yuto
  • 658
  • 2
  • 8
  • 20
  • Check here - https://stackoverflow.com/questions/38740648/how-to-set-status-bar-style-in-swift-3 & https://stackoverflow.com/questions/51042894/change-status-bar-color-dynamically-in-swift-4 – Amir Khan Mar 21 '19 at 07:17
  • Thanks you for your answer, I've tried almost all of them but they don't work in a splitviewcontroller. Do you have any other ideas? – Yuto Mar 22 '19 at 05:32

1 Answers1

5

You can use extensions below. They get preferredStatusBarStyle from embedded ViewController. You need only override preferredStatusBarStyle in your custom ViewController

extension UISplitViewController {
    open override var preferredStatusBarStyle: UIStatusBarStyle {
        let master = viewControllers.first
        return master?.preferredStatusBarStyle ?? .default
    }
}

extension UINavigationController {
    open override var preferredStatusBarStyle: UIStatusBarStyle {
        return topViewController?.preferredStatusBarStyle ?? .default
    }
}

extension UITabBarController {
    open override var preferredStatusBarStyle: UIStatusBarStyle {
        return selectedViewController?.preferredStatusBarStyle ?? .default
    }
}
Nikita Haiko
  • 371
  • 3
  • 6
  • Thank you for your answer. One question though, do I need to add this to all of my view controllers or just to the custom splitviewcontroller? – Yuto Mar 21 '19 at 23:56
  • Works great on Master/Detail project using `UISplitViewController`, thanks! – garanda Jul 25 '19 at 10:14