Charles D'Monte
Charles D'Monte

Reputation: 372

Get the position of a CGPoint( which is inside a subview), W.R.T the parentView - ObjectiveC

I have a point inside a subUIView. I want to find the location of that CGPoint with respect to the subview's parent UIView. How do I get it?

Upvotes: 1

Views: 1340

Answers (1)

Lukas Kukacka
Lukas Kukacka

Reputation: 7704

CGPoint pointInSuperview = [superview convertPoint:pointInSubview fromView:subView];

Example:

UIView *subView = [[UIView alloc] initWithFrame:CGRectMake(50, 50, 100, 100)];
[self.view addSubview:subView];

CGPoint pointInSubview = CGPointMake(20, 20);
CGPoint pointInSuperview = [self.view convertPoint:pointInSubview fromView:subView];
NSLog(@"%@", NSStringFromCGPoint(pointInSuperview));

Prints out {70, 70} to the console

Upvotes: 2

Related Questions