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

How to set Gradient color for statusbar

代码:

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

我正在尝试为状态栏设置渐变色 alone.How 以更改我的代码以设置渐变 colour.any 帮助将提前 appreciated.thanks

我就是这样处理的。

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)

那只给状态栏后面的视图添加渐变