04Creating a gradient background view
suggest changeTo create a background with a gradient you can use the CAGradientLayer class:
Swift 3.1:
func createGradient() {
let caLayer = CAGradientLayer()
caLayer.colors = [UIColor.white.cgColor, UIColor.green.cgColor, UIColor.blue.cgColor]
caLayer.locations = [0, 0.5, 1]
caLayer.frame = self.bounds
self.layer.addSublayer(caLayer)
}
Call it from viewDidLoad() like so:
override func viewDidLoad() {
super.viewDidLoad()
createGradient()
}
The CAGradientLayer colors and locations properties can take multiple values to create a gradient layer with however many colors you desire. From the documentation:
By default, the colors are spread uniformly across the layer, but you can optionally specify locations for control over the color positions through the gradient.
Found a mistake? Have a question or improvement idea?
Let me know.
Table Of Contents