
Add gradient to the view(Swift)
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 layer to view,
which we are going to create, in few lines of code
1 min readDec 18, 2018
#Step 1:
Create a CAGradientLayer
var gradientLayer: CAGradientLayer = {
let gradientLayer = CAGradientLayer()
gradientLayer.colors = [#Color1, #Color2]//Colors you want to add
gradientLayer.startPoint = CGPoint(x: 0, y: 0)
gradientLayer.endPoint = CGPoint(x: 1, y: 1)
gradientLayer.frame = CGRect.zero
return gradientLayer
}()
#Step 2:
Add this gradientLayer to layer of your view
yourView.layer.addSublayer(gradientLayer)
#Step 3
Set frame of the gradientLayer
gradientLayer.frame = yourView.bounds
Here is the output…😊
