iPhone - Get Position of UIView within entire UIWindow

134,223

Solution 1

That's an easy one:

[aView convertPoint:localPosition toView:nil];

... converts a point in local coordinate space to window coordinates. You can use this method to calculate a view's origin in window space like this:

[aView.superview convertPoint:aView.frame.origin toView:nil];

2014 Edit: Looking at the popularity of Matt__C's comment it seems reasonable to point out that the coordinates...

  1. don't change when rotating the device.
  2. always have their origin in the top left corner of the unrotated screen.
  3. are window coordinates: The coordinate system ist defined by the bounds of the window. The screen's and device coordinate systems are different and should not be mixed up with window coordinates.

Solution 2

Swift 5+:

let globalPoint = aView.superview?.convert(aView.frame.origin, to: nil)

Solution 3

Swift 3, with extension:

extension UIView{
    var globalPoint :CGPoint? {
        return self.superview?.convert(self.frame.origin, to: nil)
    }

    var globalFrame :CGRect? {
        return self.superview?.convert(self.frame, to: nil)
    }
}

Solution 4

In Swift:

let globalPoint = aView.superview?.convertPoint(aView.frame.origin, toView: nil)

Solution 5

Here is a combination of the answer by @Mohsenasm and a comment from @Ghigo adopted to Swift

extension UIView {
    var globalFrame: CGRect? {
        let rootView = UIApplication.shared.keyWindow?.rootViewController?.view
        return self.superview?.convert(self.frame, to: rootView)
    }
}
Share:
134,223

Related videos on Youtube

Abhishek Yadav
Author by

Abhishek Yadav

Updated on November 17, 2021

Comments

  • Abhishek Yadav
    Abhishek Yadav over 2 years

    The position of a UIView can obviously be determined by view.center or view.frame etc. but this only returns the position of the UIView in relation to it's immediate superview.

    I need to determine the position of the UIView in the entire 320x480 co-ordinate system. For example, if the UIView is in a UITableViewCell it's position within the window could change dramatically irregardless of the superview.

    Any ideas if and how this is possible?

    Cheers :)

  • Lior Frenkel
    Lior Frenkel about 13 years
    awesome :) I thought it should be simple, but I had the feeling apple wouldn't give it so easy... well they did. thanks
  • Lior Frenkel
    Lior Frenkel about 13 years
    I added a different question - it worked for me in simulator but not on the real device :(
  • Matt__C
    Matt__C over 12 years
    Be aware that specifying nil in the toView parameter gives you device co-ordinates, which won't be what you want if you are not in portrait orientation. See convertpointtoview-in-landscape-mode-giving-wrong-values
  • Nikolai Ruhe
    Nikolai Ruhe over 12 years
    @Matt__C: Specifying nil calculates window coordinates, not device coordinates. If you want device coordinates you have to further convert using UIWindow's conversion methods.
  • Dan Abramov
    Dan Abramov over 11 years
    This method kept returning aView.frame.origin for me. It took me a whlie to realize my view's superview did not have a superview itself.
  • user4951
    user4951 about 11 years
    If your superView did not have a superView your view should not be seen at all
  • Nikolai Ruhe
    Nikolai Ruhe over 10 years
    @JimThio To be super-precise: If the view's superview was a window it would be visible. On iOS UIWindow derives fron UIView.
  • devios1
    devios1 over 9 years
    This isn't working at all for me. Converting (0,0) on a view is giving me negative coordinates! How is that even possible?
  • Nikolai Ruhe
    Nikolai Ruhe over 9 years
    @chaiguy If the view's origin is to the left or above the screen's top left corner its window coordinates will, of course, be negative.
  • devios1
    devios1 over 9 years
    I think the problem was that the tableView must have been in a scrollView or something. I wasn't expecting that. Got it figured out.
  • Ghigo
    Ghigo over 9 years
    Adapting @Matt__C linked solution to your case, it would become: [view.superview convertPoint:view.frame.origin toView:[UIApplication sharedApplication].keyWindow.rootViewController.view]
  • trdavidson
    trdavidson over 9 years
    @Ghigo this perfectly works to get the position - how would I use this information however to set a view relative to the screen without using autolayout?
  • Reid
    Reid over 7 years
    I've been trying to do this with a UIToolbar. I need to determine the horizontal margins the system uses for bar button items so that I can adjust my layout (these margins have changed between iOS versions and are different between iPhone and iPad). For the life of me I can't figure it out...no matter what combination of views I supply as arguments and receiver to the method, origin is always (0, 7). Any tips?
  • nastassia
    nastassia about 4 years
    simple and nice!
  • famfamfam
    famfamfam about 3 years
    some time superview is not enough sir
  • Harsh Thakur
    Harsh Thakur over 2 years
    As good as it can be. Thanks