remove all subLayers from a view

IosObjective CCalayer

Ios Problem Overview


In an animation I added a lot of sublayers to a view, with:

[self.view.layer addSublayer:layer1];
[self.view.layer addSublayer:layer2];

....

I would like to remove all sublayers with an action. I already tried with this suggestion of a similar question:

rootLayer.sublayers = nil;

but it doesn't work...

Could you help me? Than you!

Ios Solutions


Solution 1 - Ios

The sublayers property of a CALayer object returns a copy of the array. Setting it no nil does nothing about the sublayers. This however will do:

for (CALayer *layer in self.view.layer.sublayers) {
    [layer removeFromSuperlayer];
}

Or, in Swift

self.view.layer.sublayers?.forEach { $0.removeFromSuperlayer() }

Solution 2 - Ios

Swift 3.0 & Swift 4.0

Set the sublayers property to nil to remove all sublayers from a view.

view.layer.sublayers = nil

also you can add

.removeAll()

Solution 3 - Ios

This worked for me and fixed the crash:

[self.view.layer.sublayers makeObjectsPerformSelector:@selector(removeFromSuperlayer)]

I changed the view with my image UImageview, and the crash is gone.

Solution 4 - Ios

Swift 2.0:

	for layer: CALayer in self.view.layer.sublayers! {
		layer.removeFromSuperlayer()
	}

or

	self.view.layer.performSelector("removeFromSuperlayer")

Solution 5 - Ios

Swift 5:

You can either remove the layer itself or iterate through them and do the following:

layer.removeAllAnimations()
layer.removeFromSuperlayer()

Solution 6 - Ios

For swift5 to remove CAShapeLayer from added view

guard let sublayers = self.view.layer.sublayers else { return }

for sublayer in sublayers where sublayer.isKind(of: CAShapeLayer.self) {
        sublayer.removeFromSuperlayer()
}

Solution 7 - Ios

Swift 4.1

self.view.layer.sublayers?.removeAll()

or if in a UIView sub-class just

layer.sublayers?.removeAll()

Solution 8 - Ios

If you want to delete all sublayers and add a new one, you can easily do:

rootLayer.sublayers = [layer1] // adding one layer

rootLayer.sublayers = [layer1, layer2, ...] // adding two or more layers

This could be helpful, if you work with tableview or collectionview cells. You don't need to call prepareForReuse for removing the sublayers.

Solution 9 - Ios

Simple one liner in SWIFT 4.

self.view.layer.sublayers.removeAll()

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionBeppino66View Question on Stackoverflow
Solution 1 - IosSvein Halvor HalvorsenView Answer on Stackoverflow
Solution 2 - IosHarshil KotechaView Answer on Stackoverflow
Solution 3 - IosHussain1982View Answer on Stackoverflow
Solution 4 - IosAlvin GeorgeView Answer on Stackoverflow
Solution 5 - IosMatias JurfestView Answer on Stackoverflow
Solution 6 - IosHardik ThakkarView Answer on Stackoverflow
Solution 7 - IosJonJView Answer on Stackoverflow
Solution 8 - IosSean StaynsView Answer on Stackoverflow
Solution 9 - IosRmalmoeView Answer on Stackoverflow