Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to change the color of status bar when gradient is applied to navigation bar?

Tags:

ios

swift

I want to apply gradient color on navigation bar. I applied that but the status bar color is not changing. Can anyone help me with this?

Here is the code for implementing the gradient color:

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)
like image 231
Dupinder kaur Avatar asked Oct 18 '22 20:10

Dupinder kaur


1 Answers

Use this:

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

output:

enter image description here

In 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)
like image 200
KSR Avatar answered Oct 20 '22 20:10

KSR