iOS 8 使状态栏变黑而不是半透明

iOS 8 make statusbar black and not translucent

所以我的应用程序中的状态栏目前看起来像这样:

我希望它看起来像这样:

2 http://mojoimage.com/free-image-hosting-12/22iOS7_StatusBar-copy.png

所以它是黑底白字。目前它在这个特定的屏幕上不起作用,因为背景不是黑色的。我已经将 pList 中的 Status Bar Style 设置为 UIStatusBarStyleLightContent

我还要补充一点,视图是初始的 ViewController,并且没有嵌入 UINavigationController

编辑:这不是重复的,因为大多数其他以前的解决方案已经过时了。

你可以把它设置为光,然后在它后面扔一个视图。如果您在您的应用中允许设备旋转,您还必须考虑设备旋转。
Swift:

override func viewDidLoad() {
    super.viewDidLoad()

    let statusBG = UIView(frame: CGRect(x: 0, y: 0, width: UIScreen.mainScreen().bounds.width, height: 21))
    statusBG.backgroundColor = UIColor.blackColor()
    view.addSubview(statusBG)
}

Objective-C:

- (void)viewDidLoad {
    [super viewDidLoad];
    UIView *statusBG = [[UIView alloc] initWithFrame:CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, 21)];
    statusBG.backgroundColor = [UIColor blackColor];
    [self.view addSubview:statusBG];
}

因为在iOS6或更早的版本上,状态栏不是半透明的,所以最好在iOS版本的基础上做这个。在你的 viewDidLoad:

[[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleLightContent];

if (SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO(@"7.0")) {
    UIView *statusBarView=[[UIView alloc] initWithFrame:CGRectMake(0, 0,[UIScreen mainScreen].bounds.size.width, 20)];
    statusBarView.backgroundColor=[UIColor blackColor];
    [self.view addSubview:statusBarView];
}

其中 SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO:

#define SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO(v)  ([[[UIDevice currentDevice] systemVersion] compare:v options:NSNumericSearch] != NSOrderedAscending)

这将为您提供一个黑色背景和白色文本的状态栏。

在你的 AppDelegate applicationDidFininshLaunchingWithOptions 方法中写在下面 code.This 有助于你的所有视图 controller.You 不需要在每个视图控制器中添加代码。

[UIApplication sharedApplication].statusBarHidden=NO;
if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 7) 
{ 
  [[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleDefault];
  [application setStatusBarHidden:NO];
  self.window.clipsToBounds=YES;
  self.window.frame =  CGRectMake(0,20,self.window.frame.size.width,self.window.frame.size.height-20);
}
else
{
  self.window=[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
  [UIApplication sharedApplication].statusBarStyle = UIStatusBarStyleLightContent;
  [application setStatusBarHidden:NO];
  self.window.clipsToBounds=YES;
  self.window.frame =  CGRectMake(0,20,self.window.frame.size.width,self.window.frame.size.height-20);
}

然后你yourprojectName.plist给

Status bar is initially hidden is NO
View controller-based status bar appearance is YES