Gradient Views w/Swift
Saturday, 19 January 2019
When you want to give the background color to the view, but you want to add more colors and get the gradient effect, you need to add a layer to view,
which we are going to create, in few lines of code.
- Create a CAGradientLayer
var gradientLayer: CAGradientLayer = {
let gradientLayer =CAGradientLayer()
gradientLayer.colors = [#Color1, #Color2] //note: colors you want to use
gradientLayer.startPoint = CGPoint(x:0 y:0)
gradientLayer.endPoint = CGPoint(x:1 y:1)
gradientLayer.frame= CGRect.zero
return gradientLayer
} ()
- Add gradientLayer to the view layer
currentView.layer.addSublayer(gradientLayer)
- Set frame of the gradientLayer
gradientLayer.frame = currentView.bounds
- Published in blog, iOS, Programming Languages, Swift
No Comments


