简体   繁体   English

iOS 8将状态栏设为黑色且不透明

[英]iOS 8 make statusbar black and not translucent

So my status bar in my app currently looks like this: 因此,我的应用程序中的状态栏当前如下所示:

1个

And I'd rather it look something like this: 我希望它看起来像这样:

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

So that it's black with white text. 因此它是黑色的,带有白色文本。 At the moment it doesn't work on this particular screen because the background isn't black. 目前,由于背景不是黑色,因此无法在该特定屏幕上使用。 And I've already set the Status Bar Style in the pList to UIStatusBarStyleLightContent . 而且我已经将pList中的Status Bar Style UIStatusBarStyleLightContentUIStatusBarStyleLightContent

I'll also add that the View is the initial ViewController AND it doesn't have a UINavigationController embedded into it: 我还要添加一个视图,它是初始的ViewController,并且没有嵌入UINavigationController

EDIT: This is not a duplicate because most of the other previous solutions are outdated already. 编辑:这不是重复的,因为大多数其他以前的解决方案已经过时了。

You can just set it to light and then toss a view behind it. 您可以将其设置为亮,然后在其后面抛一个视图。 You'll also have to account for device rotation if you allow that in your app. 如果您在应用中允许设备旋转,则还必须考虑设备旋转。
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: 目标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];
}

Because on iOS 6 or earlier, the status bar is not translucent, so It's better to do this based on the iOS version. 因为在iOS 6或更早版本上,状态栏不是半透明的,所以最好基于iOS版本执行此操作。 In your viewDidLoad : 在您的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];
}

where SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO : 其中SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO

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

This will give you a status bar with black background and white text. 这将为您提供带有黑色背景和白色文本的状态栏。

in your AppDelegate applicationDidFininshLaunchingWithOptions method write below code.This helps to your all view controller.You don't need to add code in every view controller. 在您的AppDelegate applicationDidFininshLaunchingWithOptions方法中编写以下代码。这有助于您的所有视图控制器。您无需在每个视图控制器中添加代码。

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

Then you yourprojectName.plist give 然后你yourprojectName.plist给

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM