0

I want to be able to change the status bar background color of my app, to transparent on 3 specific UIViewControllers and set the rest to be something else.

I'm not sure how to check which view controller is the current view controller. It should be an if/else statement. I have this in my AppDelegate:

extension UIApplication {
    var statusBarView: UIView? {
        return value(forKey: "statusBar") as? UIView
    }
}

 func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool
    {
        let appDelegate = UIApplication.shared.delegate as? AppDelegate

        if let currentVC = appDelegate?.window?.rootViewController?.getCurrentlyDisplayedVC()
        {

            if currentVC is LoginVC || currentVC is RegisterVC || currentVC is LostPasswordVC {
                UIApplication.shared.statusBarView?.backgroundColor = .clear
            } else {
                UIApplication.shared.statusBarView?.backgroundColor = Color_Main_Blue
            }
        }

        UIApplication.shared.statusBarStyle = .lightContent
        return true
    }
Dani
  • 3,427
  • 3
  • 28
  • 54
  • Please check my ans. https://stackoverflow.com/questions/43073623/changing-the-color-of-the-status-bar/43073806#43073806 Hope it helps you ! – byJeevan Nov 28 '17 at 11:44

2 Answers2

1

If I understand you correctly, you need to find currently displayed viewController. This extension should do a trick:

extension UIViewController {
    func getCurrentlyDisplayedVC() -> UIViewController {
        if let presentedVC = presentedViewController {
            return presentedVC.getCurrentlyDisplayedVC()
        } else if let split = self as? UISplitViewController, let last = split.viewControllers.last {
            return last.getCurrentlyDisplayedVC()
        }
        else if let nav = self as? UINavigationController, let top = nav.topViewController {
            return top.getCurrentlyDisplayedVC()
        }
        else if let tab = self as? UITabBarController {
            if let selected = tab.selectedViewController {
                return selected.getCurrentlyDisplayedVC()
            }
        }
        return self
    }
}

Then, where you need to find current viewController, you call:

let appDelegate = UIApplication.shared.delegate as? AppDelegate
if let currentVC = appDelegate?.window?.rootViewController?.getCurrentlyDisplayedVC() {
        if currentVC is YourViewController {
            //do what you need
            (UIApplication.shared.value(forKey: "statusBar") as? UIView)?.backgroundColor = UIColor.white
        } else {
            //other checks
        }
    }
Predrag Samardzic
  • 2,661
  • 15
  • 18
  • I've altered it a little bit, but ti still shows me that blue background color doesn't appear. It works for the Login, Register and Lost Password View controllers. but it doesn't changes for the rest. That's odd. I dont see what I did wrong. Please, see my updated code above – Dani Nov 28 '17 at 11:47
  • 1
    Seems that you are calling this only on app launch. You should call it each time you change your vc, otherwise statusBar will have background color that you set at the start. – Predrag Samardzic Nov 28 '17 at 13:01
0

Try with below code, Might be work for you.

override func viewWillAppear(animated: Bool) {
    supper.viewWillAppear(animated)

    UIApplication.shared.statusBarHidden = false
    UIApplication.shared.statusBarStyle = .lightContent
    let statusBar: UIView = UIApplication.shared.value(forKey: "statusBar") as! UIView
    statusBar.backgroundColor = self.view.backgroundColor // or change as you want.
}
iPatel
  • 46,010
  • 16
  • 115
  • 137