Reputation: 3875
from main viewController's ViewDidAppear
UIView* parent = [[UIView alloc] initWithFrame:CGRectMake(100, 100, 100, 100)];
UIView* child1 = [[UIView alloc] initWithFrame:CGRectMake(30, 30, 60, 60)];
UIView* child2 = [[UIView alloc] initWithFrame:CGRectMake(10, 10, 20, 20)];
[parent setBackgroundColor:[UIColor redColor]];
[child1 setBackgroundColor:[UIColor blackColor]];
[child2 setBackgroundColor:[UIColor greenColor]];
[child1 addSubview:child2];
[parent addSubview:child1];
[self.view addSubview:parent];
NSLog(@"Absolute coordinates of child2: %@",NSStringFromCGRect([child2 convertRect:child2.frame toView:nil] ));
abs point of child2: {{150, 170}, {20, 20}}
I was expecting the origins to be (140,140, 20,20).
Why the extra 10 for the X
and 30 for the Y
?
Upvotes: 1
Views: 3150
Reputation: 10195
What you need to call is:
[child2.superview convertRect:child2.frame toView:nil]
NOT
[child2 convertRect:child2.frame toView:nil]
ie. you need to call convertRect:
on the view that contains the frame you are converting.
Then you'll probably find X == 140 and Y == 160, with the extra 20 Y being the status bar given that self.view
lives within the UIWindow
beneath it.
Upvotes: 10