Gradients in Swift

How gradual is this?

To add gradient effects to views you deal with the sublayer of the UIView, rather than the UIView itself. But how does this work when we want to add gradients and us the mysterious gradient Locations.


  • Some knowledge of CoreGraphics would be useful


CAGradientLayer: A layer that draws a colour gradient over its background colour

Locations: The location where a gradient stopsBackground

Image for post


Locations are where gradient stop.

For example, in the first view above the red gradient begins at 0 (the top), and ends at 0.5 (the center).

1 is at the bottom of the relevant view.

Sample code to achieve the gradients above:

A GitHub link for this project:

Want to get in contact? Try the link here:

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store