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


当前回答

实现这一功能的关键是只有全屏视图控制器才能指定状态栏的样式。

如果你正在使用一个导航控制器,并且想要在每个视图控制器的基础上控制状态栏,你会想要子类化UINavigationController并实现preferredStatusBarStyle,这样它就会返回topViewController的首选项。

确保你将故事板场景中的类引用从muinavigationcontroller更改为你的子类(例如下面例子中的MyNavigationController)。

(以下方法对我有用。如果你的应用程序是基于TabBar,你会想做类似的事情通过子类化UITabBarController,但我还没有尝试出来)。

@interface MyNavigationController : UINavigationController

@end

@implementation MyNavigationController

- (UIStatusBarStyle)preferredStatusBarStyle
{
    return self.topViewController.preferredStatusBarStyle;
}

@end

其他回答

Swift 3 - Xcode 8。

如果你想让状态栏最初隐藏在启动屏幕上,那么试试这个,

步骤1:将以下内容添加到info.plist。

查看基于控制器的状态栏外观值NO 状态栏初始隐藏值为YES

第二步:在didFinishLaunchingWithOptions方法中写入。

UIApplication.shared.isStatusBarHidden = false
UIApplication.shared.statusBarStyle = UIStatusBarStyle.lightContent

这招对我很管用:

在plist中设置UIViewControllerBasedStatusBarAppearance为YES rootViewController需要方法实现 ——(UIStatusBarStyle) preferredStatusBarStyle

因为我的rootViewController是由Cocoapods (JASidePanelController)管理的,我通过一个类别添加了这个方法:

#import "JASidePanelController+StatusBarStyle.h"

@implementation JASidePanelController (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.

实现这一功能的关键是只有全屏视图控制器才能指定状态栏的样式。

如果你正在使用一个导航控制器,并且想要在每个视图控制器的基础上控制状态栏,你会想要子类化UINavigationController并实现preferredStatusBarStyle,这样它就会返回topViewController的首选项。

确保你将故事板场景中的类引用从muinavigationcontroller更改为你的子类(例如下面例子中的MyNavigationController)。

(以下方法对我有用。如果你的应用程序是基于TabBar,你会想做类似的事情通过子类化UITabBarController,但我还没有尝试出来)。

@interface MyNavigationController : UINavigationController

@end

@implementation MyNavigationController

- (UIStatusBarStyle)preferredStatusBarStyle
{
    return self.topViewController.preferredStatusBarStyle;
}

@end

如果你的应用程序需要有UIStatusBarStyleLightContent默认,但你仍然想有能力在某些屏幕上使用UIStatusBarStyleDefault,你可以选择在控制器级别上管理状态栏颜色,但在这种情况下,你必须在每个视图控制器中覆盖preferredStatusBarStyle(或在一个基本视图控制器中实现它,从它所有其他视图控制器将继承)。这里有另一种解决这个问题的方法:

在plist中设置UIViewControllerBasedStatusBarAppearance为NO 设置UIStatusBarStyle为UIStatusBarStyleLightContent

所有视图控制器的状态栏都将使用白色文本。现在只在需要带有黑色文本的状态栏的视图控制器中添加这些方法:

-(void)viewWillAppear:(BOOL)animated  
{  
  [super viewWillAppear:animated];  
  [[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleDefault];
}    

-(void)viewWillDisappear:(BOOL)animated  
{  
  [super viewWillAppear:animated];  
  [[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleLightContent];
}