Dmitri K. Dmitri K. - 24 days ago 5x
iOS Question

How to replace current CALAyer

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)

Which works fine and draws properly. However if i later want to change the color by clicking the second button I can no longer change the color of the layer at index 0. If I use same code nothing happens. What am i missing? And yes i call the view.setNeedsDisplay()


from the documentations The sublayers are listed in back to front 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