Removing all CALayer's sublayers

radex picture radex · Jan 14, 2010 · Viewed 54.8k times · Source

I have trouble with deleting all of layer's sublayers. I currently do this manually, but that brings unnecessary clutter. I found many topics about this in google, but no answer.

I tried to do something like this:

for(CALayer *layer in rootLayer.sublayers)
{
    [layer removeFromSublayer];
}

but it didn't work.

Also, i tried to clone rootLayer.sublayers into separate NSArray, but result was the same.

Any ideas?

Edit:

I thought it works now, but I was wrong. It works good with CALayers, but it doesn't work with CATextLayers. Any ideas?

Answer

Pascal Bourque picture Pascal Bourque · May 1, 2010

The simplest way to remove all sublayers from a layer is to set the sublayer property to nil:

rootLayer.sublayers = nil;