3

After implementing a UIViewController, it seems that the status bar content colour doesn't change (still remains black) for some reason. How can it be changed to the 'light' mode (white colour) programatically only using Swift 4.0 for just this specific UIViewController? Not the whole application.

ViewController.swift class

import UIKit

class ViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()            
        view.backgroundColor = UIColor.blue

        self.title = "Hello World"
        self.navigationController?.navigationBar.barTintColor = UIColor.gray
        self.navigationController?.navigationBar.prefersLargeTitles = true
        self.navigationController?.navigationItem.largeTitleDisplayMode = .automatic

        self.navigationController?.navigationBar.largeTitleTextAttributes = [
            NSAttributedStringKey.foregroundColor: UIColor.white,
            NSAttributedStringKey.font : UIFont.preferredFont(forTextStyle: .largeTitle)
        ]
    }

    override var preferredStatusBarStyle: UIStatusBarStyle {
        return .lightContent
    }
}

enter image description here

jake.lange's suggestion

enter image description here

7
  • @MaihanNijat I want the status bar content to be white not black. Commented Jun 2, 2018 at 15:52
  • stackoverflow.com/questions/39802420/… Commented Jun 2, 2018 at 16:02
  • 2
    Possible duplicate of Change Status Bar Background Color in Swift 3 Commented Jun 2, 2018 at 16:02
  • That's for Swift 3, I'm using Swift 4 Commented Jun 2, 2018 at 16:02
  • It does not work for 4? Commented Jun 2, 2018 at 16:03

3 Answers 3

10

Your UINavigationController is the one setting the preferredStatusBarColor. I bet if you tried presenting this VC instead of pushing it to the navigation controller you'd see the light statusbar style.

What you'll probably want to do instead is implement a custom navigation controller and override preferred status bar style.

class CustomNavController: UINavigationController {
    override var preferredStatusBarStyle: UIStatusBarStyle {
        return .lightContent;
    }
}

EDIT:

Based on comments what you're probably looking to do is set the preferred status bar color to whatever ViewController is the top most of the UINavigationController. Here's an extension that does that, with this extension you no longer need the CustomNavController class above, just used a regular UINavigationController. You will also need to override the preferred status bar style in each of your view controllers. Credit to this SO question, see here for more in depth discussions on statusbarstyle and navigation controllers: preferredStatusBarStyle isn't called

extension UINavigationController {
    open override var childViewControllerForStatusBarStyle: UIViewController? {
        return self.topViewController
    }

    open override var childViewControllerForStatusBarHidden: UIViewController? {
        return self.topViewController
    }
}
Sign up to request clarification or add additional context in comments.

9 Comments

Not working for some reason. See latest screenshot above.
Your error is this line window?.rootViewController = UINavigationController(rootViewController: CustomNavigationController()). Change to window?.rootViewController = CustomNavigationController(rootViewController: ViewController())
This error then appears: Use of unresolved identifier 'CustomNavigationController'
Sorry, that's a spelling mistake on my part. fixed line would be window?.rootViewController = CustomNavController(rootViewController: ViewController())
What about if I want the default status bar colour (black) for a different NavigationController? How can I do that?
|
2

Can you check in the Info.plist file if this flag "View controller-based status bar appearance" is set to NO. It needs to be set YES in order to allow view controller based appearance.

Comments

0

Work's for me:

override func viewWillAppear(_ animated: Bool) {
  super.viewWillAppear(animated)

  self.navigationController?.navigationBar.barStyle = .blackTranslucent
  self.navigationController?.navigationBar.titleTextAttributes = [.foregroundColor: UIColor.white]
}

override var preferredStatusBarStyle: UIStatusBarStyle {
  return .lightContent
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.