ios – 当渐变应用于导航栏时,如何更改状态栏的颜色?

我想在导航栏上应用渐变色.我申请了,但状态栏颜色没有变化.谁能帮我这个?

以下是实现渐变颜色的代码:

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, 0, 375, 64)

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

最佳答案 用这个:

gradientLayer.frame = CGRect(x:0, y:-20, width:375, height:64)

输出:

《ios – 当渐变应用于导航栏时,如何更改状态栏的颜色?》

在Swift3中:

        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 = CGRect(x:0, y:-20, width:375, height:64)
        self.navigationController?.navigationBar.layer.addSublayer(gradientLayer)
点赞