JLT
JLT

Reputation: 3172

iOS - How To Remove Previously Added Sublayers Of a UIView

I have a custom view which is a subclass of UIView. I added some sublayers to the custom view but now I want remove them.

I tried doing this:

self.layer.sublayers = nil;

But this will remove everything including the initial sublayers of the view.

Is there any way to achieve this? Or do I have to reinitialise a new custom view every time?

Note: App runs in iOS 7 and above.

Thanks!

Upvotes: 50

Views: 41218

Answers (8)

Womble
Womble

Reputation: 5290

A blend of the safest answers:

First, set the name property on each added sublayer. In this case, adding a CAShapeLayer:

private let shapeName = "shape"
let shape = ...
shape.name = shapeName
self.layer.addSublayer(shape)

Later, remove only those named sublayers:

self.layer.sublayers?
    .filter { $0.name == self.shapeName }
    .forEach { $0.removeFromSuperlayer() }

Upvotes: 0

Lindemann
Lindemann

Reputation: 3396

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

Upvotes: 0

Vanya
Vanya

Reputation: 5005

keeping reference is not cool, in some cases you can use

resultImageView.layer.sublayers?.filter{ $0 is CAShapeLayer }.forEach{ $0.removeFromSuperlayer() }

or to be more generic by using CALayer, which removes everything

Upvotes: 20

Idan Moshe
Idan Moshe

Reputation: 1535

Here is my solution for removing AVPlayerLayer without keeping a reference to it:

- (void)viewDidDisappear:(BOOL)animated {
    [super viewDidDisappear:animated];

    // Remove player layer when screen gone
    NSUInteger layerIndex = [self.view.layer.sublayers indexOfObjectPassingTest:^BOOL(__kindof CALayer * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        return [obj isKindOfClass:[AVPlayerLayer class]];
    }];
    if (layerIndex != NSNotFound) {
        AVPlayerLayer *playerLayer = self.view.layer.sublayers[layerIndex];
        [playerLayer removeFromSuperlayer];
    }
}

Upvotes: 0

mohsen
mohsen

Reputation: 5078

first of all you should add a name to the sublayer with theLayer.name property

after that you can extend the view like this:

extension UIView {
    func removeLayer(layerName: String) {
            for item in self.layer.sublayers ?? [] where item.name == layerName {
                    item.removeFromSuperlayer()
            }
        }
}

Upvotes: 10

meow2x
meow2x

Reputation: 2124

I did it in Swift 3 using popLast().

self.layer.sublayers?.popLast()

Upvotes: 19

luckystars
luckystars

Reputation: 1754

Keep a reference to the sublayer added Remove the sublayer from the super layer when not needed.

The code would be like:

Obj C:

[thesublayer removeFromSuperlayer]

Swift:

thesublayer.removeFromSuperlayer()

//thesublayer is the name of the layer you want to remove

Upvotes: 79

Kiran Jasvanee
Kiran Jasvanee

Reputation: 6554

Another way to remove specific layer from super layer is to assign unique string in layer.name property. Which you can compare later to identify and remove it out.

for layer in sublayers {
     if layer.name == "masklayer" {
          layer.removeFromSuperlayer()
     }
 }

Upvotes: 64

Related Questions