Gradients

Adding a gradient to an object is a simple task:

let gradient: CAGradientLayer = CAGradientLayer()

gradient.colors = [UIColor.blue.cgColor, UIColor.red.cgColor]
gradient.locations = [0.0 , 1.0]
gradient.startPoint = CGPoint(x: 0.0, y: 1.0)
gradient.endPoint = CGPoint(x: 1.0, y: 1.0)
gradient.frame = CGRect(x: 0.0, y: 0.0, width: self.view.frame.size.width, height: self.view.frame.size.height)

object.layer.insertSublayer(gradient, atIndex: 0)

This works perfectly for background views, but if we are talking about a gradient on a small object (like a button), it might end up with a crispy/edgy appearance. In this case, an asset might be the better option.

A tip from the Design Guidelines: "Minimal use of bezels, gradients, and drop shadows keep the interface light and airy, while ensuring that content is paramount."

Last updated