I initially create a CAGradientLayer
on my first scene in viewDidLoad. It's inserted like this:
self.view.layer.insertSublayer(myCAGradientLayer, atIndex: 0)
That works fine. But now I need to change the gradient while the app is running. The user will do something on scene 2 and come back to scene 1, where the gradient changes. I build up a new CAGradientLayer
and add to the view the same as above.
But using viewWillAppear doesn't work.
Here is an example:
let gLocations: [Float] = [0.0, 1.0]
let gLayer: CAGradientLayer = CAGradientLayer()
gLayer.colors = [UIColor.yellowColor().CGColor, UIColor.greenColor().CGColor]
gLayer.frame = self.view.bounds
gLayer.locations = gLocations
self.view.layer.insertSublayer(gLayer, atIndex: 0)
Add the above to viewWillAppear. Then add it into a several conditionals using different colors. Only the first conditional chosen appears. Any of the other colors in the other conditionals doesn't appear.
I think part of the issue is that I'm always doing:
self.view.layer.insertSublayer(gLayer, atIndex: 0)
Otherwise, the gradient will appear on top of other UI components. I'm using several different gradients and they all use the above code. But I'm guessing they all can't be inserted at index=0.
Is there some other way to change the gradient?