Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to convert SubView's Frame Coordinate System to Self View's Coordinate System

I create programmatically one scrollView and some buttons inside there. When I click the any button have to show a popover.

My button's origin in self.view is like (100,11) and inside scrollView (9,11) and scrowView is in somewhere in self.view. The popover shows in (9,11) but right one would be (100,11). I try use convert without success.

-(IBAction)showPopover:(id)sender{
//... implemented popover above

//Wrong Origin:
NSLog(@"wrong x:%f y:%f",[sender frame].origin.x, [sender frame].origin.y);

//Transform to correct
CGRect frame = [self.view convertRect:[sender frame] toView:nil];

//Shoulf be right, but is not...
NSLog(@"new x:%f y:%f",frame.origin.x, frame.origin.y);
}

Anyone cam help me?

like image 502
rcmstark Avatar asked Jul 27 '12 22:07

rcmstark


2 Answers

A view's frame is already in the superview's coordinate system. So if your setup is self.view contains scrollview contains sender:

CGRect frame = [sender.superview convertRect:sender.frame toView:self.view];

// or, better:

CGRect frame = [sender convertRect:sender.bounds toView:self.view];
like image 142
Nikolai Ruhe Avatar answered Nov 14 '22 16:11

Nikolai Ruhe


Swift:

let frame = sender.convert(sender.bounds, to: self.view)
like image 18
Hemang Avatar answered Nov 14 '22 17:11

Hemang