How to change the color of the status bar in one view controller using swift?

UIApplication.sharedApplication().setStatusBarStyle(UIStatusBarStyle.LightContent, animated: true) 

I use this parameter to change the status bar to include all applications. But now I need to change it in only one view controller back to black. How can i do this?

+9
source share
9 answers

Set View controller-based status bar appearance in your .plist project to NO

Use viewWillAppear and viewWillDisappear to set and reset statusBarStyle, while maintaining the property with the previous statusBarStyle status, like this

 let initialStatusBarStyle : UIStatusBarStyle func viewWillAppear(animated: Bool) { super.viewWillAppear(animated) initialStatusBarStyle = UIApplication.sharedApplication().statusBarStyle UIApplication.sharedApplication().setStatusBarStyle(.LightContent, animated: animated) } func viewWillDisappear(animated: Bool) { super.viewWillDisappear(animated) UIApplication.sharedApplication().setStatusBarStyle(initialStatusBarStyle, animated: animated) } 
+12
source

Xcode 8.1, Swift 3 Solution with @IBDesignable

This solution is slightly different:

  • UIViewController subclass for centralizing logic
  • No code for viewDidLoad or viewDidDisappear
  • Uses @IBDesignable so that you can set the status bar color in the Attributes Inspector on the storyboard

Step 1 - Info.plist Setup File

Info.plist

Step 2 - Subclass UIViewController

 import UIKit @IBDesignable class DesignableViewController: UIViewController { @IBInspectable var LightStatusBar: Bool = false override var preferredStatusBarStyle: UIStatusBarStyle { get { if LightStatusBar { return UIStatusBarStyle.lightContent } else { return UIStatusBarStyle.default } } } } 

Step 3 - Inherit from DesignableViewController

Change the code for ViewController (s) from:

 class ViewController: UIViewController { 

To:

 class ViewController: DesignableViewController { 

Step 4 - Set Your Preferences on the Storyboard

Select ViewControllers on the storyboard and go to the attribute inspector: Attributes Inspector

Step 5 - Run the project and test

In my project, I set up a tab bar controller with two view controllers and switch between them. It seems good to me. Status bar Dark status bar

+10
source

It is decided:
Swift 3.1

Just using this code in the view controller:

 override var preferredStatusBarStyle: UIStatusBarStyle { return .lightContent } 
+6
source

Swift 3

Display the status bar status in the control panel in your project .plist to NO

 override func viewWillDisappear(_ animated: Bool) { super.viewWillDisappear(animated) UIApplication.shared.setStatusBarStyle(.default, animated: animated) } override func viewWillAppear(_ animated: Bool) { super.viewWillAppear(animated) UIApplication.shared.setStatusBarStyle(.lightContent, animated: animated) } 
+2
source

Objective-C answer:

 -(void)viewWillDisappear:(BOOL)animated { [super viewWillDisappear:animated]; [UIApplication sharedApplication].statusBarStyle = UIStatusBarStyleDefault; } -(void)viewWillAppear:(BOOL)animated { [super viewWillAppear:animated]; [UIApplication sharedApplication].statusBarStyle = UIStatusBarStyleLightContent; } 
+1
source
  let color = UIColor(red:0.00, green:0.60, blue:0.48,alpha:1.0) UINavigationBar.appearance().tintColor = UIColor.blue UINavigationBar.appearance().barTintColor = color 

OR

 self.navigationController?.navigationBar.titleTextAttributes = [NSForegroundColorAttributeName: UIColor.white] 
0
source

If you derived your view controllers from a common view controller, then you can simply do this as follows:

Step 1: Add this key to the info.plist file of your application.

enter image description here

Step 2: override this in a regular view controller (or ParentViewController ).

 override var preferredStatusBarStyle: UIStatusBarStyle { if self is YourChildViewController { return .lightContent } return .default } 

It! No more fancy stuff.

0
source

When you are in a navigation controller that will not be called. The navigation controller of the preferred StatusBarStyle will be called. Try this with your code:

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

and also write this:

 override var preferredStatusBarStyle: UIStatusBarStyle { return .lightContent } 
0
source

In Swit4, this works fine in my navbar based project

  let app = UIApplication.shared let statusBarHeight: CGFloat = app.statusBarFrame.size.height let statusbarView = UIView(frame: CGRect(x: 0, y: 0, width: UIScreen.main.bounds.size.width, height: statusBarHeight)) statusbarView.backgroundColor = UIColor.red view.addSubview(statusbarView) 
0
source

Source: https://habr.com/ru/post/1241714/


All Articles