Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to remove deepest sublayer from view?

What would be the method of removing the deepest sublayer from a view's main layer? Should be pretty simple, I'm very new to iOS.

like image 731
jab Avatar asked May 17 '26 21:05

jab


2 Answers

If you really mean the layer (not view) in the back, you can try this as well:

[[view.layer.sublayers objectAtIndex:0] removeFromSuperlayer];

If you want to remove the back view instead, you can use the similar:

[[view.subviews objectAtIndex:0] removeFromSuperview];

Of course, if you really want to remove the deepest sublayer/subview, including those of child views, you'll have to do something like this:

CALayer *layer = view.layer;
while ([layer.sublayers count] > 0) {
    layer = [layer.sublayers objectAtIndex:0];
}
[layer removeFromSuperlayer];

Still, that seems like a bad idea. If you're really looking for that, I'd recommend rethinking what you're actually looking for.

like image 163
Alexis King Avatar answered May 19 '26 11:05

Alexis King


If you mean the back most subview, use:

UIView *subViewToBeRemoved = [mainView.subviews objectAtIndex:0];
[subViewToBeRemoved removeFromSuperview]; 

mainView.subviews will return an array of the view's subviews in their visible order on screen.

If by deepest you mean the front most view you could use

UIView *subViewToBeRemoved = [mainView.subviews lastObject];
[subViewToBeRemoved removeFromSuperview]; 
like image 38
Darren Avatar answered May 19 '26 11:05

Darren



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!