Change the color of the status bar

I am trying to change the color of the status bar to blue or some other color.

Is this possible, or does Apple not allow it?

+11
ios xcode swift statusbar uistatusbar
source share
5 answers

NOTE. This solution does not work in iOS 13 and later.

First in Plist set View controller-based status bar appearance to NO

 func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool { // Override point for customization after application launch. let statusBar: UIView = UIApplication.shared.value(forKey: "statusBar") as! UIView if statusBar.responds(to:#selector(setter: UIView.backgroundColor)) { statusBar.backgroundColor = UIColor.blue } UIApplication.shared.statusBarStyle = .lightContent return true } 

Screenshot below

enter image description here

+14
source share

No, this is not possible with ready-made public APIs.

But with the release of iOS 7 you can change the appearance of the status bar. Therefore, I publish my decision.

From a separate view controller, overriding preferredStatusBarStyle :

 -(UIStatusBarStyle)preferredStatusBarStyle { return UIStatusBarStyleLightContent; } 

In addition, you can set the status bar style using the UIApplication statusBarStyle method. To do this, insert a new key called "View the appearance of the status bar based on the controller" and set the value to "NO". enter image description here

By disabling the "Controller-based status bar view", you can set the status bar style using the following code.

 [[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleLightContent]; 

UINavigationBar , change the hue color of the UINavigationBar property as shown below

 [[UINavigationBar appearance] setBarTintColor:[UIColor blueColor]]; 
+8
source share


You can set the background color for the status bar during application startup or during viewDidLoad of your view controller.

 extension UIApplication { var statusBarView: UIView? { return value(forKey: "statusBar") as? UIView } } // Set upon application launch, if you've application based status bar class AppDelegate: UIResponder, UIApplicationDelegate { var window: UIWindow? func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool { UIApplication.shared.statusBarView?.backgroundColor = UIColor.red return true } } or // Set it from your view controller if you've view controller based statusbar class ViewController: UIViewController { override func viewDidLoad() { super.viewDidLoad() UIApplication.shared.statusBarView?.backgroundColor = UIColor.red } } 



Here is the result:

enter image description here

Here is the Apple Guidelines / Instruction on changing the status bar. Only Dark & ​​light (while & black) are allowed in the status bar.

Here's how to change the style of the status bar:

If you want to set the status bar style, application level, then set UIViewControllerBasedStatusBarAppearance to NO in your .plist file.

if you want to set the style of the status bar, at the level of the view controller do the following:

  1. Set UIViewControllerBasedStatusBarAppearance to YES in the .plist file if you need to set the status bar style only at the UIViewController level.
  2. Add a function to setNeedsStatusBarAppearanceUpdate - setNeedsStatusBarAppearanceUpdate

  3. override the preferred StatusBarStyle in your view controller.

-

 override func viewDidLoad() { super.viewDidLoad() self.setNeedsStatusBarAppearanceUpdate() } override var preferredStatusBarStyle: UIStatusBarStyle { return .lightContent } 

Set the .plist value to match the status bar style setting. enter image description here

+6
source share

Here is my workaround: create a UIView , add it to the root view of the view controller as the artificial background
UIView

 // status bar height is 20.0pt CGRect frame = CGRectMake(0.0, 0.0, [UIScreen mainScreen].bounds.size.width, 20.0); UIView *fakeStatusBarBG = [[UIView alloc] initWithFrame:frame]; fakeStatusBarBG.backgroundColor = [UIColor yourColor]; 

2. Add it to the root mode of the view controller

 // self is your view controller, make sure fakeStatusBarBG is the top subview in your view hierarchy [self.view insertSubview:fakeStatusBarBG aboveSubview:yourTopSubview]; 

There is your move.

3. (Optional) Change the color of the contents in the status bar, only white or black.

 - (UIStatusBarStyle)preferredStatusBarStyle { if (youWantWhiteColor) { return UIStatusBarStyleLightContent; } return UIStatusBarStyleDefault; } 

This workaround does not use a private API, so you are safe. :-)

+4
source share

I made this extension to change the color of the status bar

 public extension UIViewController { func setStatusBar(color: UIColor) { let tag = 12321 if let taggedView = self.view.viewWithTag(tag){ taggedView.removeFromSuperview() } let overView = UIView() overView.frame = UIApplication.shared.statusBarFrame overView.backgroundColor = color overView.tag = tag self.view.addSubview(overView) } } 

Here is the use of the viewcontroller anywhere:

setStatusBar(color: .red)

+1
source share

All Articles