title: replace CALayer
body: <p>I am following <a href="https://stackoverflow.com/questions/422066/gradients-on-uiview-and-uilabels-on-iphone/1931498#1931498">this answer</a> to create a gradient using a <code>CALayer</code>. The
  key command is</p>
  
  <pre><code>[view.layer insertSublayer:gradient atIndex:0];
  </code></pre>
  
  <p>My problem is that I would like to update the gradient regularly. If I call the above code repeatedly, then nothing happens as new gradients are placed under the old ones. See
  <a href="https://stackoverflow.com/questions/1759460/is-calayer-insertsublayer-atindex-destructive">this answer</a>.</p>
  
  <p>My question is: how does one replace a sublayer with another one. I should probably call
  <code>replaceSublayer:with:</code>. But to do this, I need a reference to the old gradient. Is there a dynamic way of obtaining this. Something like <code>[view.layer getSublayerAtIndex: 0]</code>? Is the easiest way to just store a reference to the current gradient in the viewcontroller?</p>
  
  <p>Ideas?</p>