如何使用swift在特定视图中更改状态栏颜色?

时间:2015-07-10 09:59:06

标签: ios swift statusbar

InViewDidLoad
    UIApplication.sharedApplication().statusBarStyle = .LightContent

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

        UIApplication.sharedApplication().statusBarStyle = UIStatusBarStyle.Default

    }

通过参考我已经实现的上述链接。但状态栏颜色不会改变只有状态栏上的文本正在改变。 Changing the Status Bar Color for specific ViewControllers using Swift in iOS8

2 个答案:

答案 0 :(得分:0)

如果您的应用使用导航栏,则状态栏颜色将更改为与导航栏的颜色相匹配。

如果没有导航栏,您可以在状态栏后面放置一个20磅高的视图(例如一些填充颜色的视图),这将是用户看到的颜色。

我找到了上述信息in this article

要在swift中执行此操作,可能会执行以下操作:

// make a view tall enough to fit under the status bar
var statusBarView: UIView = UIView(frame: CGRectMake(0.0, 0.0, 320.0, 20.0))

// give it some color
statusBarView.backgroundColor = UIColor.redColor()

// and add it to your view controller's view
view.addSubview(statusBarView) 

答案 1 :(得分:0)

默认状态栏后面没有单独的背景视图。它实际上只是覆盖在主视图的顶部,因此背景采用了这种颜色。

因此,如果您在导航控制器中嵌入了一个视图,它将只采用导航栏的颜色。如果没有,那么它将采用主视图的颜色。

您可以创建一个位于状态栏下方顶部的单独视图,并设置此背景。

正如迈克尔所说,以下代码应该这样做,但我已将宽度更改为设备的宽度,因此它适用于所有情况。

var statusBarView: UIView = UIView(frame: CGRectMake(0.0, 0.0, UIScreen.mainScreen().bounds.width, 20.0))
statusBarView.backgroundColor = UIColor.redColor()
view.addSubview(statusBarView)