Add a Gradient Layer Below a UITableView

It is possible!

Steven Curtis
3 min readAug 17, 2021
YouTube link

This one really annoyed me. Placing a CAGradientLayer() in the background of a UITableView should be super super easy, right? Well, not quite. Let us take a look.

Difficulty: Beginner | Easy | Normal | Challenging

This article has been developed using Xcode 12.5, and Swift 5.4

Prerequisites:

The output

The idea of this project is to create a `UITableView` with a gradient right behind it. The resultant project will run in the simulator with something like the following:

It should be simple but…

The challenge

Essentially here is a simple UITableViewController which, in this case, displays the Strings “a”, “b”, “c”, “d” and “e”.

The issue is putting the CAGradientLayer into place.

If you’d like to get that natty green to yellow transition like in the image above, you might have a gradient layer expressed like the following:

--

--