I have a simple application in swift. It has a function that returns a CALayer which is a layer with gradient background.
When a button is clicked I use this code:
view.layer.insertSublayer(myGradientLayer, at: 0)
from the documentations
The sublayers are listed in back to front order.so each time you add a layer at index 0 then it will be below your previous one. The first layer works with you because it's the only layer but any layer you put at index 0 after it will be below the first layer