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?
Calling
rootLayer.sublayers = nil;
can cause a crash (e.g. if, under iOS 8, you call removeFromSuperview twice on the view owningrootLayer
).The right way should be:
[[rootLayer.sublayers copy] makeObjectsPerformSelector:@selector(removeFromSuperlayer)]
The call to
copy
is needed so that the array on whichremoveFromSuperlayer
is iteratively called is not modified, otherwise an exception is raised.How about using reverse enumeration?
Because the group in sublayers are changed during enumeration, if the order is normal. I would like to know the above code's result.