Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

iOS - Get location of a view in a window?

I have a UIView that displays a popup after it's been clicked. The popup needs to be added to the main UIWindow to make sure that it goes on top of everything else.

I want the position of this popup to be relative to my UIView, so I need to know the relative location of my UIView in the window.

Question: How can I find the location of a UIView in a UIWindow when the UIView is not directly in the UIWindow (It's inside the view of my viewController)?

like image 828
aryaxt Avatar asked Aug 11 '11 15:08

aryaxt


3 Answers

Use can use the UIView method covertRect:toView to convert to the new co-ordinate space. I did something very similar:

// Convert the co-ordinates of the view into the window co-ordinate space
CGRect newFrame = [self convertRect:self.bounds toView:nil];

// Add this view to the main window
[self.window addSubview:self];
self.frame = newFrame;

In my example, self is a view that is being removed from its superView and added to the window over the top of where it was. The nil parameter in the toView: means use the window rather than a specific view.

Hope this helps,

Dave

like image 164
Magic Bullet Dave Avatar answered Sep 22 '22 03:09

Magic Bullet Dave


You can ask your .superview to convert your origin for you for you

CGPoint originGlobal = [myView.superview convertPoint:myView.frame.origin 
                                               toView:nil];
like image 20
Yunus Nedim Mehel Avatar answered Sep 21 '22 03:09

Yunus Nedim Mehel


Does UIView's convertPoint:toView: not work? So:

CGPoint windowPoint = [myView convertPoint:myView.bounds.origin toView:myWindow];
like image 31
Craig Avatar answered Sep 21 '22 03:09

Craig