iOS - Get location of a view in a window?

45,008

Solution 1

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

Solution 2

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

CGPoint originGlobal = [myView.superview convertPoint:myView.frame.origin 
                                               toView:nil];

Solution 3

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

CGPoint windowPoint = [myView convertPoint:myView.bounds.origin toView:myWindow];

Solution 4

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

Solution 5

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) }
}
Share:
45,008
aryaxt
Author by

aryaxt

Updated on November 09, 2020

Comments

  • aryaxt
    aryaxt over 3 years

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

  • Magic Bullet Dave
    Magic Bullet Dave almost 13 years
    Perfect. There are several of these types of method for converting to and from co-ordinate spaces if you need them.
  • AntiMoron
    AntiMoron almost 8 years
    Does this work when the view is inside an UIScrollView
  • Pavan
    Pavan about 7 years
    @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?
  • Magic Bullet Dave
    Magic Bullet Dave about 7 years
    It should work, also see the nil parameter means use the window rather than a specific view, so no need to traverse.
  • Typewriter
    Typewriter almost 7 years
    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?
  • Magic Bullet Dave
    Magic Bullet Dave almost 7 years
    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.
  • turnipinrut
    turnipinrut over 4 years
    this was very helpful
  • user3069232
    user3069232 over 3 years
    ader I would delete this answer if I were you. Lots of down votes..