swift ios 尝试更改某些 VC 上状态栏的颜色 [英] swift ios trying to change color of status bar on certain VC's

查看:27
本文介绍了swift ios 尝试更改某些 VC 上状态栏的颜色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的应用程序中,我有 4 个 ViewController,其中两个我将状态栏从白色更改为黑色,如下所示:

In my app I have 4 ViewController and in two of them I am changing the status bar from white to black like this:

override func viewWillAppear(animated: Bool) {
        super.viewWillAppear(animated)

        UIApplication.sharedApplication().statusBarStyle = UIStatusBarStyle.Default
    }

    override func viewWillDisappear(animated: Bool) {
        super.viewWillDisappear(animated)

        UIApplication.sharedApplication().statusBarStyle = .LightContent
    }

问题是,如果我在两个都有状态栏上方代码的 ViewController 之间切换,首先会变成黑色,这是正确的,但是当进入另一个 ViewController 时它又会变成白色.

The problem is if I switch between two ViewController that both have the code above the status bar will first change to black which is right, but then it changes to white again when entering the other ViewController.

如何使某些 ViewController 上的状态栏保持白色?

How can I keep the status bar white on certain ViewController's ?

推荐答案

尝试在您的 VC 中添加以下方法.使用 .default 或 .lightContent 来更改状态栏颜色.我使用 Xcode 8 和 swift 3 进行了测试:

Try to add the following method in your VC's. Use .default or .lightContent to change the status bar color. I tested using Xcode 8 and swift 3:

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

我使用 Xcode 7.3.1 和 swift 2.3 创建了一个新的选项卡式应用程序.我有两个与 FirstViewController 和 SecondViewController 相关的类的选项卡.在 FirstViewController 中,我添加了以下方法:

I created a new tabbed application with Xcode 7.3.1 and swift 2.3. I have two tabs with the classes associated FirstViewController and SecondViewController. In FirstViewController I added the following method:

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

在 SecondViewController 中,我将背景更改为黑色并添加了以下方法:

And In the SecondViewController, I changed the background to black and I added the following method:

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

最后,我在 FirstViewController 中添加了两个按钮.一个按钮以模态方式呈现控制器,另一个按钮通过推动呈现.当我以模态方式呈现视图时,'preferredStatusBarStyle' 工作但当我通过推送呈现时,我需要添加以下代码行:

Finally, I added two buttons in the FirstViewController. One button present a controller modally and the other button present through push. When I presented the view modally the 'preferredStatusBarStyle' work but when I presented through push I need to add the following line of code:

self.navigationController?.navigationBar.barStyle = .Black

这篇关于swift ios 尝试更改某些 VC 上状态栏的颜色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆