Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Any method to get layer's parent view?

Tags:

ios

view

calayer

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.

like image 720
Adithya Avatar asked Apr 09 '14 06:04

Adithya


2 Answers

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

like image 72
Léon Pelletier Avatar answered Sep 18 '22 12:09

Léon Pelletier


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.

like image 23
Rajneesh071 Avatar answered Sep 18 '22 12:09

Rajneesh071