Yes, you read it right "layer's parent view". I'm not sure if its the right term to use but what I mean is, I have a layer added as sublayer of a view. I wanted to know if there is any way to get the view from the layer.
This question is quite old, but accessing CALayer
's parent view is easy as:
layer.delegate
If you look into a debugger, you'll see that the delegate
property is populated with the UIView
containing the Layer, and if not, you can recursively check until you get the main CALayer
directly hosted in the view.
Source: https://developer.apple.com/documentation/quartzcore/calayer/1410984-delegate
You can perform this using KVC.
UIView *View1=[UIView new];
View1.tag=1;
[View1.layer setValue:View1 forKey:@"LayerObject"];
UIView *View2=[UIView new];
View2.tag=2;
[View2.layer setValue:View2 forKey:@"LayerObject"];
NSLog(@"Your Layers superView = %@",[View2.layer.superlayer valueForKey:@"LayerObject"]);
This will return your layer's parent View.
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