35

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)?

aryaxt
  • 76,198
  • 92
  • 293
  • 442

8 Answers8

69

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

Magic Bullet Dave
  • 9,006
  • 10
  • 51
  • 81
  • Perfect. There are several of these types of method for converting to and from co-ordinate spaces if you need them. – Magic Bullet Dave Aug 12 '11 at 07:50
  • 1
    Does this work when the view is inside an UIScrollView – AntiMoron Jul 14 '16 at 07:18
  • @AntiMoron, does it? Given the time that has passed, or are you supposed to traverse through converting from one frame to another and then the window? – Pavan May 03 '17 at 13:42
  • It should work, also see the nil parameter means use the window rather than a specific view, so no need to traverse. – Magic Bullet Dave May 03 '17 at 16:52
  • Shouldn't the convertRect method be passed self.frame, instead of self.bounds? Why would you want to convert the rect of your bounds, which (nearly) always has origin 0,0? – Typewriter Jul 27 '17 at 21:38
  • The origin of the frame is relative to the superview. The convertRect method is coverting the coordinates from your view's co-ordinate system to the supplied view's one. In this case we want the whole view's rectangle in the window, hence we want (0,0) - (width, height). Other times we might want to find the co-ordinates of a rect within the view (like the bounding box of a touch), Hope that makes sense. – Magic Bullet Dave Jul 28 '17 at 06:09
15

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

CGPoint originGlobal = [myView.superview convertPoint:myView.frame.origin 
                                               toView:nil];
Yunus Nedim Mehel
  • 12,089
  • 4
  • 50
  • 56
8

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

CGPoint windowPoint = [myView convertPoint:myView.bounds.origin toView:myWindow];
Craig
  • 3,253
  • 5
  • 29
  • 43
7

I needed to do this in Xamarin.iOS

For anyone looking for a solution there, what eventually worked for me was:

CGRect globalRect = smallView.ConvertRectToView(smallView.Bounds, rootView);
Stoyan Berov
  • 1,171
  • 12
  • 18
3

Swift 5 utility

extension UIView {
    var originOnWindow: CGPoint { return convert(CGPoint.zero, to: nil) }
}

Usage

let positionOnWindow = view.originOnWindow

For UIScrollView it is slightly different

extension UIScrollView {
    var originOnWindow: CGPoint { return convert(contentOffset, to: nil) }
}
WantToKnow
  • 1,767
  • 2
  • 12
  • 18
1

Below Xamarin.iOS Implementation Worked For Me.

    CGRect GetRectAsPerWindow(UIView view)
    {
        var window = UIApplication.SharedApplication.KeyWindow;
        return view.Superview.ConvertRectToView(view.Frame, window);
    }

Pass in the view whose frame you want with respect to Window.

soan saini
  • 230
  • 3
  • 9
0
extension UIView {
    var globalFrame: CGRect {
        return convert(bounds, to: window)
    }
}
AKIL KUMAR
  • 287
  • 3
  • 8
  • Please don't post only code as answer, but also provide an explanation what your code does and how it solves the problem of the question. Answers with an explanation are usually more helpful and of better quality, and are more likely to attract upvotes. – Mark Rotteveel Jul 11 '22 at 08:25
-8
CGRect myFrame = self.superview.bounds;
ader
  • 5,403
  • 1
  • 21
  • 26