sxynyw
Last Updated: February 25, 2016
·
3.251K
· cybersamx
Portrait sam

Find the coordinate of a subview in the coordinate space of a UIView higher in the view hierarchy

Problem

You have a subview nested in a view hierarchy and want to find the new coordinate of the subview in the coordinate system in another view.

Solution

Say you created a view hierarchy like this:

UIView *subview1 = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 20, 20, 300, 300);
[self.view addSubview:subview1];
UIView *subview2 = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 30, 30, 200, 200);
[superview addSubview:vsubview2];

Picture

To find the coordinate of subview in the view controller view coordinate system, use the following code:

CGPoint point = [subview1 convertPoint:subview2.frame.origin toView:self.view];

Reference

UIView Class Reference

Say Thanks
Respond