xcode 更改状态栏颜色 Objective-C

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/42366675/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 09:48:06  来源:igfitidea点击:

Change status bar color Objective-C

iosobjective-cxcode

提问by pilou

In my app, there is a dark mode, and I would like to know how to change the status bar color? Thanks,

在我的应用程序中,有一个黑暗模式,我想知道如何更改状态栏颜色?谢谢,

UIColor *color = [UIColor whiteColor];
self.navigationController.navigationBar.barTintColor = color;

回答by Ronak Chaniyara

May be below lines of code in didFinishLaunchingWithOptionsmethod in AppDelegate.mcould help:

可能在didFinishLaunchingWithOptions方法中的以下代码行AppDelegate.m可能会有所帮助:

[[UIApplication sharedApplication] setStatusBarHidden:NO];
[[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleLightContent];

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

if ([statusBar respondsToSelector:@selector(setBackgroundColor:)]) {

    statusBar.backgroundColor = [UIColor whiteColor];//set whatever color you like
}

回答by Joe Razon

Ok, according to Apple DocssetStatusBarStyle:animated:is deprecated since iOS 9.0, and you should now put this code instead:

好的,根据Apple DocssetStatusBarStyle:animated:自 iOS 9.0 以来已被弃用,您现在应该改为使用以下代码:

-(UIStatusBarStyle)preferredStatusBarStyle 
{
return UIStatusBarStyleLightContent;
}

Good luck and happy coding! :)

祝你好运,编码愉快!:)

回答by daris mathew

A simple and effective way is to add a property to the info.plistand set the status bar style as UIStatusBarStyleLightContent.

一个简单有效的方法是在 中添加一个属性info.plist并将状态栏样式设置为UIStatusBarStyleLightContent

This works like a charm!

这就像一个魅力

What we should do is:

我们应该做的是:

  • First open the info.plistfile of your app, then add a property UIViewControllerBasedStatusBarAppearanceand set its value to NO
  • Next head to your Appdelegateor any class file and use the following code:

    UIApplication.sharedApplication.statusBarStyle = UIStatusBarStyleLightContent;
    

    Another way to write the code is:

    [[UIApplication sharedApplication] setStatusBarStyle: UIStatusBarStyleLightContent];
    
  • 首先打开info.plist您的应用程序文件,然后添加一个属性UIViewControllerBasedStatusBarAppearance并将其值设置为NO
  • 接下来转到您的Appdelegate或任何类文件并使用以下代码:

    UIApplication.sharedApplication.statusBarStyle = UIStatusBarStyleLightContent;
    

    编写代码的另一种方法是:

    [[UIApplication sharedApplication] setStatusBarStyle: UIStatusBarStyleLightContent];
    

回答by Dvole

Swift 3 solution:
In view controller that needs non-default status bar appearance, use this:

Swift 3 解决方案:
在需要非默认状态栏外观的视图控制器中,使用:

override var preferredStatusBarStyle: UIStatusBarStyle {
    return .lightContent
}

回答by Swetha Lakshmi

Add these lines in plist

在 plist 中添加这些行

<key>UIStatusBarStyle</key>
<string>UIStatusBarStyleLightContent</string>
<key>UIViewControllerBasedStatusBarAppearance</key>
<false/>

回答by Frostmourne

Since iOS 13 came out, we have to resort to new properties in view of status bar, place following code under

自从iOS 13出来,我们不得不求助于新的属性status bar,将下面的代码放在

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions

if (@available(iOS 13.0, *)) {

        UIView *statusBar = [[UIView alloc]initWithFrame:[UIApplication sharedApplication].keyWindow.windowScene.statusBarManager.statusBarFrame];

        if ([statusBar respondsToSelector:@selector(setBackgroundColor:)]) {

            statusBar.backgroundColor = [UIColor whiteColor];

        }

        [[UIApplication sharedApplication].keyWindow addSubview:statusBar];



    } else {
        UIView *statusBar = [[UIView alloc]initWithFrame:[UIApplication sharedApplication].keyWindow.frame];

        if ([statusBar respondsToSelector:@selector(setBackgroundColor:)]) {

            statusBar.backgroundColor = [UIColor whiteColor];

        }

    }

This takes care of the crash that takes place in iOS 13 and sets frame for other iOS versions too

这会处理 iOS 13 中发生的崩溃,并为其他 iOS 版本设置框架

回答by Narasimha Nallamsetty

This is what worked for me.

这对我有用。

click on navigationBar -> style -> Default Then select Translucent and below that Bar Tint -> (select any colour you want)

单击导航栏 -> 样式 -> 默认然后选择半透明,然后在 Bar Tint 下方 ->(选择您想要的任何颜色)