将状态栏设置为黑色

Set the status bar to black colour

我想将 status bar(顶部显示时钟和电池的小条)的颜色设置为黑色背景和白色文本。我该怎么做?

我目前的方法:

我在 info.plist

中添加了以下内容
View controller-based status bar appearance --> NO

并在 viewController

- (UIStatusBarStyle)preferredStatusBarStyle
{
    return UIStatusBarStyleDefault;
}

不过,风格没有变。如何让状态栏变黑?

您可以调用以下命令使文本变白:

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

状态栏背景透明。没有办法改变这一点。您可以在其下方固定一个视图作为背景。

您需要按照 Xcode-8.1 和 iOS 10 中的 3 个步骤进行操作:

这样终于可以得到想要的结果了:

对于Xcode 8 / iOS 10 / Swift 3:

通过在状态栏下添加一个彩色的 UIView 来临时更改一个视图上的状态栏背景颜色。我将这些行放在 viewWillAppear() 中。我的色调是白色,透明度为 45%,但您可以输入任何不透明或透明的 UIColor 值。

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)

如果视图上有导航控制器,则可以将 statusView 添加到 navigationController 而不是视图。

模拟状态栏背景视图的宽度也可以传入UIScreen.main.bounds.size.width。不要硬编码宽度,否则它不会适合所有设备。

或者,您也可以使用此代码更改整个应用程序的状态栏背景。请注意,即使放置在 viewWillAppear() 而不是 viewDidLoad(),当您导航 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)

转到 Plist 文件

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;
}

它为我修复了状态栏颜色。希望对你有帮助。

简单的解决方案:

转到您的主故事板(应用启动时加载的故事板,启动完成后),并将顶层 UIView 的背景颜色更改为黑色。

如果您需要屏幕的其余部分有另一个彩色背景,只需创建另一个 UIView 作为顶层视图的子视图,将其框架设置为在顶层布局指南下方开始,并将其颜色设置为您需要的任何颜色。

您仍然需要设置其他答案中提到的 plist 标志。