Set the status bar to black colour

女生的网名这么多〃 提交于 2019-11-28 11:40:47

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)

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.

Shrawan

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

So finally we can get the desired result:

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.

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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!