Set the status bar to black colour

2019-01-20 01:56发布

问题:

I want to set the colour of the status bar (The small strip on top where the clock and battery is displayed) to black background with white text. How can I do this ?

My approach so far :

I added the following in the info.plist

View controller-based status bar appearance --> NO

and in the viewController

- (UIStatusBarStyle)preferredStatusBarStyle
{
    return UIStatusBarStyleDefault;
}

However, the style does not change. How can I make the status bar black?

回答1:

For Xcode 8 / iOS 10 / Swift 3:

Change the status bar background color temporarily on one view by adding a colored UIView under the status bar. I put these lines in viewWillAppear(). my tint is white with 45% transparency, but you can put in any opaque or transparent UIColor value.

let statusView = UIView(frame: CGRect(x: 0, y: 0, width:     self.view.bounds.width, height: 20))
statusView.backgroundColor = UIColor.white.withAlphaComponent(0.45)
 self.view.addSubview(statusView)

You can add your statusView to the navigationController, instead of the view, if you have a navigation controller on the view.

You can also pass in UIScreen.main.bounds.size.width for the width of the simulated status bar background view. Do not hardcode a width, otherwise it wont fit all devices properly.

Alternatively, you can also change the status bar background throughout your app using this code. be aware that if even if placed in viewWillAppear(), not viewDidLoad(), other views will still adopt the altered status bar when you navigate forward/back.

let statWindow = UIApplication.shared.value(forKey:"statusBarWindow") as! UIView
let statusBar = statWindow.subviews[0] as UIView
statusBar.backgroundColor = UIColor.white.withAlphaComponent(0.45)


回答2:

You can make the text white by calling:

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

The status bar has a transparent background. There is no way of changing that. You can pin a view underneath it to act as a background.



回答3:

You need to follow 3 steps in Xcode-8.1 and iOS 10:

So finally we can get the desired result:



回答4:

Easy solution:

Go to your main storyboard (the one that gets loaded when the app starts, after the splash is done), and change the background color of the top-level UIView to black.

If you need the rest of the screen to have another colored background, just create another UIView as a subview of the top-level view, set its frame to start beneath the top-layout-guide, and set its color to whatever color you need.

You would still need to set the plist flags as mentioned in other answers.



回答5:

Goto Plist file

a) Set the value "View controller-based status bar appearance" to NO
b) Set the value "Status bar style" to UIStatusBarStyleLightContent 

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    // Override point for customization after application launch.
#define SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO(version)  ([[[UIDevice currentDevice] systemVersion] compare:version options:NSNumericSearch] != NSOrderedAscending)

    if (SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO(@"7.0"))
    {
        UIView *view=[[UIView alloc] initWithFrame:CGRectMake(0, 0,[UIScreen mainScreen].bounds.size.width, 20)];
        view.backgroundColor=[UIColor blackColor];
        [self.window.rootViewController.view addSubview:view];
    }
    return YES;
}

It fixed status bar color for me. Hope it helps.