简体   繁体   中英

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?

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];

迅速:

let frame = sender.convert(sender.bounds, to: self.view)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM