iOS CAGradientLayer Creating a CGGradientLayer with multiple colors.

Help us to keep this website almost Ad Free! It takes only 10 seconds of your time:
> Step 1: Go view our video on YouTube: EF Core Bulk Insert
> Step 2: And Like the video. BONUS: You can also share it!

Example

// View to hold the CAGradientLayer.
let view: UIView = UIView(frame: CGRect(x: 0, y: 0, width: 320, height: 320))
    
// Initialize gradient layer.
let gradientLayer: CAGradientLayer = CAGradientLayer()
    
// Set frame of gradient layer.
gradientLayer.frame = view.bounds
    
// Color at the top of the gradient.
let topColor: CGColor = UIColor.blue.cgColor
    
// Color at the middle of the gradient.
let middleColor: CGColor = UIColor.yellow.cgColor
    
// Color at the bottom of the gradient.
let bottomColor: CGColor = UIColor.green.cgColor
    
// Set colors.
gradientLayer.colors = [topColor, middleColor, bottomColor]
    
// Set locations of the colors.
gradientLayer.locations = [0.0, 0.5, 1.0]
    
// Insert gradient layer into view's layer heirarchy.
view.layer.insertSublayer(gradientLayer, at: 0)

Result :

Complex CAGradientLayer.



Got any iOS Question?