我的应用程序背景是黑色的,但在iOS 7中,状态栏变成了透明的。所以我什么也看不见,只有角落里的绿色电池指示灯。如何将状态栏文本颜色改为白色,就像在主屏幕上一样?


当前回答

简单地在应用程序委托:

[[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleLightContent];

在Swift 5中,遵循以下步骤:

在Info.plist中添加key UIViewControllerBasedStatusBarAppearance并将value设置为false 添加键UIStatusBarStyle和设置值UIStatusBarStyleLightContent

其他回答

如果我使用UINavigationController,我在iOS 9和Swift 2.0中做这个

self.navigationController?.navigationBar.barStyle = UIBarStyle.Black

如果我使用模态segue

override func preferredStatusBarStyle() -> UIStatusBarStyle {
    return .LightContent
}

如果你想将它设置为任何颜色,请使用下面的代码。

id statusBarWindow = [[UIApplication sharedApplication] valueForKey:@"statusBarWindow"];
id statusBar = [statusBarWindow valueForKey:@"statusBar"];

SEL setForegroundColor_sel = NSSelectorFromString(@"setForegroundColor:");
if([statusBar respondsToSelector:setForegroundColor_sel]) {
    // iOS 7+
    [statusBar performSelector:setForegroundColor_sel withObject:YourColorHere];
                                                                 ^^^^^^^^^^^^^
}

我知道我正在访问私有API,但我已经在许多项目中使用了这个,苹果已经批准了它。

在提交应用程序时,将此代码发送到Apple的评论部分,并告知您正在使用此代码更改状态栏的颜色。

是的,不要忘记下面的内容。

非常简单的方法来改变状态栏的颜色。 创建navigation Controller的子类。

在view didload方法中编写以下代码。 在所有视图控制器中执行此代码

self.navigationBar.titleTextAttributes = @{NSForegroundColorAttributeName :
                                                                        [UIColor whiteColor],
                                               NSFontAttributeName:[UIFont boldSystemFontOfSize:19]};

总结一下,编辑你的项目信息。Plist并添加:

基于视图控制器的状态栏外观:NO

状态栏样式:不透明的黑色样式

或者如果你有raw key/value plist

uiviewcontrollerbasedstatusbar外观:NO

UIStatusBarStyle:不透明黑色风格

简单地在应用程序委托:

[[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleLightContent];

在Swift 5中,遵循以下步骤:

在Info.plist中添加key UIViewControllerBasedStatusBarAppearance并将value设置为false 添加键UIStatusBarStyle和设置值UIStatusBarStyleLightContent