如何为状态栏设置渐变颜色

时间:2017-12-26 08:27:58

标签: swift3 cagradientlayer

代码:

extension UIApplication {
    class var statusBarBackgroundColor: UIColor? {
        get {
            return (shared.value(forKey: "statusBar") as? UIView)?.backgroundColor
        } set {
            (shared.value(forKey: "statusBar") as? UIView)?.backgroundColor = newValue
        }
    }
}

UIApplication.statusBarBackgroundColor = .blue

我正在尝试单独为状态栏设置渐变颜色。如何更改我的代码以设置渐变颜色。任何帮助都将受到赞赏。谢谢提前

1 个答案:

答案 0 :(得分:0)

这就是我管理它的方式。

let gradientLayer = CAGradientLayer()

    gradientLayer.frame = CGRect(x:0, y:-20, width:375, height:64)
    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
    gradientLayer.colors = [ colorTop, colorBottom]
    gradientLayer.locations = [ 0.0, 1.0]
    gradientLayer.frame = CGRect(x:0, y:-20, width:375, height:self.view.frame.height * 0.06)

    let window: UIWindow? = UIApplication.shared.keyWindow
    let view = UIView()
    view.backgroundColor = UIColor.clear
    view.translatesAutoresizingMaskIntoConstraints = false
    view.frame = CGRect(x: 0, y: 0, width: (window?.frame.width)!, height: self.view.frame.height * 0.07)
    view.layer.addSublayer(gradientLayer)
    window?.addSubview(view)

只将渐变添加到状态栏后面的视图