StatusBar Style updates with some delay

232 Views Asked by At

I have 2 StoryBoard in my project and all ViewController in my 1st storyBoard are of .lightContent statusBarStyle and all ViewController in my 2nd storyBoard are of .default statusBarStyle.

For that i have done below steps.

1.View controller-based status bar appearance is true

2 Already use below code.

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

When i set 2nd storyBoard ViewController as rootController from 1st storyBoard statusBar style changing but i am facing below issue.

When i set 1st storyBoard ViewController as rootController, statusBarStyle update after few seconds.

Not Proper

enter image description here

After Few Seconds

enter image description here

here is my demo link : https://www.dropbox.com/s/ijqg73zm1jxbokc/statusBarDemo.zip?dl=0

UPDATE

MY First ViewController Code

override func viewWillAppear(_ animated: Bool) {
    super.viewWillAppear(animated)
    self.navigationController?.setNavigationBarHidden(true, animated: animated)
}

override var preferredStatusBarStyle: UIStatusBarStyle {
    return .default
}

@IBAction func btnInitialControllerTapped(_ sender: Any) {
    let controller = UIStoryboard(name: "Initial", bundle: nil).instantiateViewController(withIdentifier: "InitialViewController") as! InitialViewController
    let navController = UINavigationController.init(rootViewController: controller)
    appDelegate.window?.rootViewController = navController
}

MY Second ViewController Code

override var preferredStatusBarStyle: UIStatusBarStyle {
    return .lightContent
}

@IBAction func btnControllerTapped(_ sender: Any) {
    let storyboard = UIStoryboard(name:"Main", bundle: nil)
    let vc = storyboard.instantiateViewController(withIdentifier: "ViewController") as! ViewController
    let navigationController = UINavigationController(rootViewController: vc)
    appDelegate.window?.rootViewController = navigationController
}

Please guide me what i'm missing or what i have implemented wrongly. Any help will be appreciated. Thanks

1

There are 1 best solutions below

3
Kasım Özdemir On

InitialViewController:

var status = true
override var preferredStatusBarStyle: UIStatusBarStyle {
    return status ? .lightContent : .default
}

@IBAction func btnControllerTapped(_ sender: Any) {
    self.status = false
    self.preferredStatusBarStyle
    self.setNeedsStatusBarAppearanceUpdate()
    let storyboard = UIStoryboard(name:"Main", bundle: nil)
    let vc = storyboard.instantiateViewController(withIdentifier: "ViewController") as! ViewController
    let navigationController = UINavigationController(rootViewController: vc)
    appDelegate.window?.rootViewController = navigationController
}

I found a solution like this. It works well but I don't know how useful it is.I just think it's better to change it without go on the other screen.