create a white background which gradients to light blue in one corner and to light purple in another corner in swift

You can achieve this effect by using a CAGradientLayer with startPoint and endPoint properties to define the direction of the gradient.

Here's an example code snippet:

main.swift
let gradientLayer = CAGradientLayer()
gradientLayer.colors = [UIColor.white.cgColor, UIColor(red: 0.8, green: 0.85, blue: 0.95, alpha: 1.0).cgColor, UIColor(red: 0.9, green: 0.8, blue: 0.9, alpha: 1.0).cgColor]
gradientLayer.locations = [0.0, 0.5, 1.0]
gradientLayer.startPoint = CGPoint(x: 0, y: 0)
gradientLayer.endPoint = CGPoint(x: 1, y: 1)

let backgroundView = UIView(frame: view.bounds)
backgroundView.layer.addSublayer(gradientLayer)
view.addSubview(backgroundView)
474 chars
10 lines

This code creates a CAGradientLayer with three colors that blend from white to light blue and finally to light purple. The locations array specifies where each color starts and stops in the gradient, and the startPoint and endPoint properties define the direction of the gradient.

You can adjust the colors and locations to get the exact gradient you want, and use this layer as the background for any view.

related categories

gistlibby LogSnag