简体   繁体   English

Swift如何为一个视图而不是所有视图更改状态栏的背景颜色

[英]Swift how to change the background color of status bar lise navigation bar for a view, not all view

I added a navigation bar in a view, but the background color of status bar is not the same with my navigation bar. 我在视图中添加了导航栏,但是状态栏的背景颜色与我的导航栏不同。 like this: 像这样: 在此处输入图片说明

I searched informations on internet, I can find how to change the color of status bar, but I can not find how to change the background color like the color of navigation bar... Could you help to resolve this problem : to set the background color of status bar like color of navigation bar for a view ? 我在互联网上搜索了信息,可以找到如何更改状态栏的颜色,但是却找不到像导航栏的颜色那样更改背景颜色的方法...您能帮助解决此问题:设置背景的状态条像一个视图导航栏的颜色? Thank you. 谢谢。

您可以使用:

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

You can change the color of status bar if you are using the gradient layer.Here is the code just try it and do let me know. 如果您使用的是渐变层,则可以更改状态栏的颜色。以下是代码,请尝试一下并告诉我。

let colorTop =  UIColor(red: 69/255, green: 90/255, blue: 195/255, alpha: 1.0).CGColor
    let colorBottom = UIColor(red: 230/255, green: 44/255, blue: 75/255, alpha: 1.0).CGColor

    let gradientLayer = CAGradientLayer()
    gradientLayer.colors = [ colorTop, colorBottom]
    gradientLayer.locations = [ 0.0, 1.0]
    gradientLayer.frame = CGRectMake(0, -20, 375, 64)

  self.navigationController?.navigationBar.layer.addSublayer(gradientLayer)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM