luke luke - 6 months ago 60
iOS Question

how to make a faded Navigation Bar

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.


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.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)