Dmitri K. Dmitri K. - 1 year ago 104
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()

Answer Source

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

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download