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.
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.
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];
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With