如何在 swift 2/ iOS 9 中正确更改状态栏样式?

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

how do I properly change my status bar style in swift 2/ iOS 9?

iosswiftstatusbar

提问by John Hymanson

I'm attempting to change my status bar's style to .Lightbut the previous code I implemented in swift 1.2 seems not to work anymore.. here's the code:

我正在尝试将状态栏的样式更改为,.Light但我在 swift 1.2 中实现的先前代码似乎不再起作用了..这是代码:

override func viewDidLoad() {
        super.viewDidLoad()

        UIApplication.sharedApplication().statusBarStyle = .LightContent

    }

now I have my View controller-based status bar appearanceinfo.plist setting to YES, and reading the UIKit doc, this will negate any statusBarStyle changes and keep it at default. However when I change the setting to 'NO' and change the statusBarStyle, I get this <Error>: CGContextSaveGState: invalid context 0x0. If you want to see the backtrace, please set CG_CONTEXT_SHOW_BACKTRACE environmental variablein my debugger.. So is this a bug in Xcode? because to change the status bar style you must change info.plist setting to NO, but when that happens.. error

现在我将View controller-based status bar appearanceinfo.plist 设置为 YES,并阅读 UIKit 文档,这将否定任何 statusBarStyle 更改并将其保留为默认值。但是,当我将设置更改为“NO”并更改 statusBarStyle 时,我<Error>: CGContextSaveGState: invalid context 0x0. If you want to see the backtrace, please set CG_CONTEXT_SHOW_BACKTRACE environmental variable在调试器中得到了这个。这是 Xcode 中的错误吗?因为要更改状态栏样式,您必须将 info.plist 设置更改为 NO,但是当发生这种情况时.. 错误

回答by Tom Hughes

Apple have added the capability to change the status bar style in the deployment info. Simply choose 'Light'.ScreenShot of Xcode

Apple 在部署信息中添加了更改状态栏样式的功能。只需选择“光”。Xcode 截图

Also set View controller-based status bar appearancekey to NOin the Info.plist

也将View controller-based status bar appearance键设置NOInfo.plist

info plist

信息列表

回答by dede.exe

I always did this way.

我一直都是这样做的。

class ViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.
    }

    //Changing Status Bar
    override func preferredStatusBarStyle() -> UIStatusBarStyle {

        //LightContent
        return UIStatusBarStyle.LightContent

        //Default
        //return UIStatusBarStyle.Default
    }
}

It works in any swift 2.x version. This requires that you set View controller-based status bar appearancein your Info.plistfile to YES.

它适用于任何 swift 2.x 版本。这要求您View controller-based status bar appearanceInfo.plist文件中设置为YES.

回答by Tom Hughes

You can still use preferredStatusBarStylein your view controller:

您仍然可以preferredStatusBarStyle在视图控制器中使用:

step 1: in the info.plist set ViewControllerBasedStatusBarAppearance to YES. step 2: add this code to the ViewController you'd like to edit :

第 1 步:在 info.plist 中将 ViewControllerBasedStatusBarAppearance 设置为 YES。第 2 步:将此代码添加到您要编辑的 ViewController 中:

override func  preferredStatusBarStyle() -> UIStatusBarStyle {

    return UIStatusBarStyle.LightContent
}

*** Tip: It seems to only work outside of the ViewDidLoad(), didReceiveMemoryWarning() functions.

*** 提示:它似乎只能在 ViewDidLoad()、didReceiveMemoryWarning() 函数之外工作。

回答by MRustamzade

Swift 3 just add View controller-based status bar appearancewith value NOto info.plistand then add to ViewControllerwhere you want:

斯威夫特3只加View controller-based status bar appearance用价值NOinfo.plist,然后添加到ViewController你想去的地方:

UIApplication.shared.statusBarStyle = UIStatusBarStyle.lightContent

回答by Yuliia

The change in deployment info works but despite - you need to add the 'View controller-based status bar appearance' key to plist file setting it to NO.

部署信息的更改有效,但尽管 - 您需要将“基于视图控制器的状态栏外观”键添加到 plist 文件中,将其设置为 NO。

回答by Tom Hughes

You can also just add this in the AppDelegate. This option is better if you want to change it for every ViewController in the app and not have to make it different for every other VC.

您也可以在 AppDelegate 中添加它。如果您想为应用程序中的每个 ViewController 更改它,而不必为每个其他 VC 更改它,则此选项更好。

func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
    application.statusBarStyle = UIStatusBarStyle.LightContent
    // instead of 
    // UIApplication.sharedApplication().setStatusBarStyle(UIStatusBarStyle.LightContent, animated: false)
    // which gives warning about deprecation in iOS 9

    return true
}

回答by David M

It looks like it's a bug in Xcode 7.0. I'm also getting the Error>: CGContextSaveGState: invalid context 0x0.error when setting View controller-based status bar appearance

看起来这是 Xcode 7.0 中的一个错误。Error>: CGContextSaveGState: invalid context 0x0.设置的时候也报错View controller-based status bar appearance

For now I'm just overriding the status bar color in every view controller.

现在我只是覆盖每个视图控制器中的状态栏颜色。

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

回答by Telstar

You can choose "light" in the deployment info, but then you also need to add the "View controller-based status bar appearance" and set it to NO.

您可以在部署信息中选择“light”,但是您还需要添加“基于视图控制器的状态栏外观”并将其设置为 NO。

回答by Anirudh R.Huilgol.

Here try this it might help you

在这里试试这个它可能会帮助你

First goto info.plist file and add this "View controller-based status bar appearance" as a key and set the value as NO

首先转到 info.plist 文件并将此“基于视图控制器的状态栏外观”添加为键并将值设置为 NO

here below shown in the imageenter image description here

如下图所示在此处输入图片说明

after this come to AppDelegate.swift file and past this UIApplication.sharedApplication().statusBarStyle = UIStatusBarStyle.LightContentline of code in

在这之后来到 AppDelegate.swift 文件并通过这 UIApplication.sharedApplication().statusBarStyle = UIStatusBarStyle.LightContent行代码

func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool{
 UIApplication.sharedApplication().statusBarStyle =   UIStatusBarStyle.LightContent
 return true
}

like this

像这样

回答by bikram sapkota

For swift 3 override the preferredStatusBarStyle variable use this:

对于 swift 3 覆盖 preferredStatusBarStyle 变量使用这个:

 override var preferredStatusBarStyle: UIStatusBarStyle{
    return .lightContent
}