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


当前回答

改变所有viewcontroller的状态栏文本颜色

斯威夫特3

if在Info.plist中查看基于控制器的状态栏外观= YES

然后使用这个扩展为所有NavigationController

extension UINavigationController
{
    override open var preferredStatusBarStyle: UIStatusBarStyle {
    return .lightContent
     }
 }

如果没有UINavigationController,只有UIViewController,那么使用下面的代码:

extension UIViewController
{
    override open var preferredStatusBarStyle: UIStatusBarStyle {
    return .lightContent
     }
 }

objective - c

创建类别类

对于UIView控制器

在ui + StatusBarStyle.h

 @interface UIViewController (StatusBarStyle)
 @end

在ui + StatusBarStyle.m

 #import "UIViewController+StatusBarStyle.h"

 @implementation UIViewController (StatusBarStyle)
 -(UIStatusBarStyle)preferredStatusBarStyle
 {
  return UIStatusBarStyleLightContent;
 }
 @end 

对于UINavigationController

在UINavigationController + StatusBarStyle.h

 @interface UINavigationController (StatusBarStyle)
 @end

在UINavigationController + StatusBarStyle.m

 #import "UINavigationController+StatusBarStyle.h"

 @implementation UINavigationController (StatusBarStyle)
 -(UIStatusBarStyle)preferredStatusBarStyle
 {
  return UIStatusBarStyleLightContent;
 }
 @end  

其他回答

Xcode似乎一直在改变这个,所以这是最新的。

截至2021年,Swift 5, Xcode 12

设置状态栏为白色。

打开你的Info.plist。 添加键UIViewControllerBasedStatusBarAppearance并将值设置为No (false)。人类可读的版本是“基于视图控制器的状态栏外观”。 添加键UIStatusBarStyle和设置值UIStatusBarStyleLightContent(即“轻内容”)。

这在iOS 7 UI过渡指南中有记录,你需要一个苹果开发者ID才能直接访问。相关节选:

因为状态栏是透明的,所以它后面的视图是透视的。[…使用UIStatusBarStyle常量来指定状态栏内容应该是暗的还是亮的: UIStatusBarStyleDefault显示黑色内容。[…] UIStatusBarStyleLightContent显示轻内容。当状态栏后面是黑色内容时使用。

也可能令人感兴趣:

在iOS 7中,你可以通过单个的vew控制器控制状态栏的样式,并在应用程序运行时更改它。要选择这种行为,添加UIViewControllerBasedStatusBarAppearance键到应用程序的信息。plist文件并赋值YES。

我强烈建议你浏览一下这个文档,同样,你可以用你的苹果开发者ID访问这个文档。

改变所有viewcontroller的状态栏文本颜色

斯威夫特3

if在Info.plist中查看基于控制器的状态栏外观= YES

然后使用这个扩展为所有NavigationController

extension UINavigationController
{
    override open var preferredStatusBarStyle: UIStatusBarStyle {
    return .lightContent
     }
 }

如果没有UINavigationController,只有UIViewController,那么使用下面的代码:

extension UIViewController
{
    override open var preferredStatusBarStyle: UIStatusBarStyle {
    return .lightContent
     }
 }

objective - c

创建类别类

对于UIView控制器

在ui + StatusBarStyle.h

 @interface UIViewController (StatusBarStyle)
 @end

在ui + StatusBarStyle.m

 #import "UIViewController+StatusBarStyle.h"

 @implementation UIViewController (StatusBarStyle)
 -(UIStatusBarStyle)preferredStatusBarStyle
 {
  return UIStatusBarStyleLightContent;
 }
 @end 

对于UINavigationController

在UINavigationController + StatusBarStyle.h

 @interface UINavigationController (StatusBarStyle)
 @end

在UINavigationController + StatusBarStyle.m

 #import "UINavigationController+StatusBarStyle.h"

 @implementation UINavigationController (StatusBarStyle)
 -(UIStatusBarStyle)preferredStatusBarStyle
 {
  return UIStatusBarStyleLightContent;
 }
 @end  

对我来说,什么都没用。我试图改变状态栏颜色在ViewController2,这是嵌入在NavigationController,这反过来,从ViewController1被模态地呈现。这种方法行不通:

override var preferredStatusBarStyle: UIStatusBarStyle {
    return .darkContent
}

什么都没有发生,直到我找到了这个解决方案: 将这一行添加到ViewController1

导航控制器。modalPresentationCapturesStatusBarAppearance = 真正的

let navigationController = UINavigationController(rootViewController: viewController2)
navigationController.modalPresentationStyle = .overFullScreen
navigationController.modalTransitionStyle = .crossDissolve           
navigationController.modalPresentationCapturesStatusBarAppearance = true
self.present(navigationController, animated: true)

所以如果你的导航方案类似于ViewController1呈现的ViewController2,尝试modalPresentationCapturesStatusBarAppearance属性呈现的一个

文档:

The default value of this property is false. When you present a view controller by calling the present(_:animated:completion:) method, status bar appearance control is transferred from the presenting to the presented view controller only if the presented controller's modalPresentationStyle value is UIModalPresentationStyle.fullScreen. By setting this property to true, you specify the presented view controller controls status bar appearance, even though presented non-fullscreen. The system ignores this property’s value for a view controller presented fullscreen.

对于Xcode 5.1:

在.plist的NO中添加“基于视图控制器的状态栏外观”。

在AppDelegate中添加:

[[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleLightContent];

这是所有!