iPhone - Get Position of UIView within entire UIWindow
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...
- don't change when rotating the device.
- always have their origin in the top left corner of the unrotated screen.
- 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)
}
}
Related videos on Youtube
Abhishek Yadav
Updated on November 17, 2021Comments
-
Abhishek Yadav over 2 years
The position of a
UIView
can obviously be determined byview.center
orview.frame
etc. but this only returns the position of theUIView
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 theUIView
is in aUITableViewCell
it's position within the window could change dramatically irregardless of the superview.Any ideas if and how this is possible?
Cheers :)
-
Lior Frenkel about 13 yearsawesome :) I thought it should be simple, but I had the feeling apple wouldn't give it so easy... well they did. thanks
-
Lior Frenkel about 13 yearsI added a different question - it worked for me in simulator but not on the real device :(
-
Matt__C over 12 yearsBe 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 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 over 11 yearsThis 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 about 11 yearsIf your superView did not have a superView your view should not be seen at all
-
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 fronUIView
. -
devios1 over 9 yearsThis 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 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 over 9 yearsI 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 over 9 yearsAdapting @Matt__C linked solution to your case, it would become:
[view.superview convertPoint:view.frame.origin toView:[UIApplication sharedApplication].keyWindow.rootViewController.view]
-
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 over 7 yearsI'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 about 4 yearssimple and nice!
-
famfamfam about 3 yearssome time superview is not enough sir
-
Harsh Thakur over 2 yearsAs good as it can be. Thanks