UIApplication.sharedApplication().setStatusBarStyle() 在 iOS 9 中被弃用

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/32501305/
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-08-31 07:35:09  来源:igfitidea点击:

UIApplication.sharedApplication().setStatusBarStyle() deprecated in iOS 9

iosxcodeswiftstatusbarios9

提问by Rick

I have been using

我一直在使用

UIApplication.sharedApplication().setStatusBarStyle()

In my appDelegate and it has worked fine, but since iOS 9, this method is deprecated and I can't find an alternative.

在我的 appDelegate 中,它运行良好,但自 iOS 9 以来,此方法已被弃用,我找不到替代方法。

I want to change the statusbar style to .LightContent for my whole application, but the only suggestion xCode gives me is to handle this in every VC separately with;

我想将整个应用程序的状态栏样式更改为 .LightContent,但 xCode 给我的唯一建议是在每个 VC 中分别处理此问题;

override func preferredStatusBarStyle() -> UIStatusBarStyle {
    return .LightContent
}

Has anyone an idea how to do this for the whole application?

有没有人知道如何为整个应用程序做到这一点?

Thanks in advance

提前致谢

回答by Rick

I think I have found a solution. I ended up setting the

我想我已经找到了解决办法。我最终设置了

View controller-based status bar appearanceboolean to NO

View controller-based status bar appearance布尔值 NO

In my info.plist file.

在我的 info.plist 文件中。

Info.Plist

信息列表

Then I went to my target's General settings -> Deployment infoand changed the dropdown option Status Bar Styleto Lightinstead of Default

然后我去我的目标的General settings -> Deployment info,改变了下拉选项 Status Bar StyleLight代替Default

Change Status Bar Style to Light

将状态栏样式更改为浅色

This changed the statusbar style to Lightfor my whole application, just what I wanted.

这改变了Light我整个应用程序的状态栏样式,正是我想要的。

I Hope this helps!

我希望这有帮助!

回答by EgzonArifi

In Swift 3 is like that:

在 Swift 3 中是这样的:

 UIApplication.shared.statusBarStyle = .lightContent

回答by Marlon Ruiz

In swift 3.

在迅捷3。

In your view controller:

在您的视图控制器中:

override var preferredStatusBarStyle: UIStatusBarStyle {
    return UIStatusBarStyle.lightContent
}

If you wish when the app run your launch screen also has the status bar in lightContent then:

如果您希望应用程序运行时您的启动屏幕在 lightContent 中也有状态栏,则:

enter image description here

在此处输入图片说明

回答by Muhammad Waqas

To dynamically update UIStatusBarStyleon view controllers use this method

要在视图控制器上动态更新UIStatusBarStyle,请使用此方法

this will also remove deprecated warning

这也将删除不推荐使用的警告

'setStatusBarStyle:' is deprecated: first deprecated in iOS 9.0 - Use -[UIViewController preferredStatusBarStyle]

'setStatusBarStyle:' 已弃用:在 iOS 9.0 中首次弃用 - 使用 -[UIViewController preferredStatusBarStyle]

for calling

打电话

[[UIApplication sharedApplication] setStatusBarStyle:style];

Let's Get Started

让我们开始吧

Objective - C

目标-C

define UtilityFunction

定义UtilityFunction

+(void)setStatusBarStyle:(UIStatusBarStyle )style {    
    [[NSUserDefaults standardUserDefaults] setInteger:style forKey:@"UIStatusBarStyle"];
    [[NSUserDefaults standardUserDefaults] synchronize];
}

over-ride this method in your BaseViewController

在您的BaseViewController 中覆盖此方法

- (UIStatusBarStyle)preferredStatusBarStyle {
    UIStatusBarStyle style = [[NSUserDefaults standardUserDefaults] integerForKey:@"UIStatusBarStyle"];
    return style;
}

set UIStatusBarStyle value for the AnyViewControllerusing a UtilityFunction like below:

使用 UtilityFunction为AnyViewController设置 UIStatusBarStyle 值,如下所示:

[UtilityFunctions setStatusBarStyle:UIStatusBarStyleDefault];

// call below code for preferred style
[self preferredStatusBarStyle];

Swift 4.0

斯威夫特 4.0

define UtilityFunction

定义UtilityFunction

class func setPreferedStyle(style:UIStatusBarStyle)->Void {
    UserDefaults.standard.set(style, forKey: "UIStatusBarStyle")
    UserDefaults.standard.synchronize()
}

over-ride this method in your BaseViewController

在您的BaseViewController 中覆盖此方法

override var preferredStatusBarStyle: UIStatusBarStyle {
    if let style: UIStatusBarStyle = UIStatusBarStyle(rawValue:UserDefaults.standard.integer(forKey: "UIStatusBarStyle")) {
        return style
    }
    return UIStatusBarStyle.lightContent
}

set UIStatusBarStyle value for the AnyViewControllerusing a UtilityFunction like below:

使用 UtilityFunction为AnyViewController设置 UIStatusBarStyle 值,如下所示:

Utility.setPreferedStyle(style: .lightContent)

// call below code for preferred style
preferredStatusBarStyle()

回答by Caleb Kleveter

This worked fine for me in Xcode 7.

这在 Xcode 7 中对我来说效果很好。

In AppDelegate:

在 AppDelegate 中:

UIApplication.sharedApplication().statusBarStyle = .LightContent

回答by aremvee

for those still working with Swift 3 in Xcode 8:

对于那些仍在 Xcode 8 中使用 Swift 3 的人:

( slightly different to Marlon Ruiz's answer above, not an override function, but within viewDidLoad )

(与上面 Marlon Ruiz 的回答略有不同,不是覆盖函数,而是在 viewDidLoad 中)

override func viewDidLoad() {
    super.viewDidLoad()

    var preferredStatusBarStyle: UIStatusBarStyle {
        return .lightContent
    }
}

回答by Despotovic

In info.plist, set: View controller-based status bar appearanceboolean to NO

在 info.plist 中,设置: View controller-based status bar appearanceboolean 为NO

In app delegate's didFinishLaunchingWithOptions, use function parameter application(and not the [UIApplication sharedApplication]or simillary the UIApplication.sharedApplication()in swift) to set this like so:

在 app delegate's 中didFinishLaunchingWithOptions,使用函数参数application(而不是swift 中的[UIApplication sharedApplication]UIApplication.sharedApplication()类似的)来设置它,如下所示:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    application.statusBarStyle = UIStatusBarStyleLightContent;
}

回答by Guilherme Colares

This is the new way in AppDelegate:

这是 AppDelegate 中的新方法:

UIApplication.sharedApplication().setStatusBarStyle(UIStatusBarStyle.LightContent, animated: true)