Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to make a faded Navigation Bar

Tags:

xcode

ios

swift

How does one make a navigation bar that fades out? the top having an alpha of say 0.5, with the bottom half being 0. As it goes from top to bottom, the alpha decreases and it becomes more transparent.

Something like this: enter image description here

As you can see, it becomes more transparent as you go down the nav bar.

like image 806
luke Avatar asked Oct 08 '16 09:10

luke


1 Answers

Try to use CAGradientLayer for this purpose. I've tested and works. For Swift 3.0.

let gradient: CAGradientLayer = CAGradientLayer()

// put colors into an array, from top to bottom
gradient.colors = [UIColor.black.withAlphaComponent(0.5).cgColor, UIColor.clear.cgColor]
gradient.frame = view.frame

// setting direction and stop points - from top to bottom
gradient.startPoint = CGPoint(x: 0, y: 0)
gradient.endPoint = CGPoint(x: 0, y: 0.5)

yourView.layer.insertSublayer(gradient, at: 0)
like image 192
pedrouan Avatar answered Oct 11 '22 08:10

pedrouan