Is there an easy way of getting the absolute point of UIView? By absolute I mean the location of the UIView relative to 1024x768 on an iPad?
I need an easy way because I have a UIButton inside a UITableViewCell inside a UITableView inside a view presented by a viewcontroller that is a child of another view controller repeating to the fifth or sixth view controller. And due to this complex design I need the absolute rect of the UIButton so I can display an UIActionSheet on the "main" viewcontroller.
What you are looking for is:
This will converts a point from the receiver’s coordinate system (your button) to that of the specified view (top level view).
Documentation
So I ended up using UITapGestureRecognizer instead and calculating the position like this:
To convert from a view's local frame or path to SCREEN coordinates, use the built-in converters:
UIAccessibilityConvertPathToScreenCoordinates
or
UIAccessibilityConvertFrameToScreenCoordinates
This does return a path or rect, but you can extract points from those.
Swift 4 convert that seems to work for a more than two levels of subviews:
I found the convert routine somewhat confusing. So, I brute force tested all the possible combinations with 5 levels of views. With the results posted on github
[Edit] When rootView is a UITableView, adjust for contentOffset:
If you want to get the coordinates of a
CGPoint
/CGRect
in a certainUIView
you can use theUIView
methodsor for
CGRect
in the first argument put the
CGPoint
/CGRect
you want to convert and in the second put theUIView
you want to convert toSo, we have a let's call it small view that has a certain point inside it. That point has local coordinates relative to its parent view that is small view. Now, we want to know, what would be the coordinates of that point if it would stay in the same spot on the screen, but it would be part of the root view. (this effectively gives us the coordinates relative to the whole screen.)
You will calculate the global coordinates of that point
localCoordinatesOfThePoint
is also a CGPoint structure.Another scenario
is when you have the whole screen covered with root view, this has a large view, (still smaller than the root view though), and this large view has a small view as a subview inside it.
You will calculate the position of the small view's tip
self.smallview.origin
is a CGPoint relative to Large view. Thus, we calculate, what would be its coordinates if this point (it's the top-left tip of small view really) would be inside the root view.CATCH
If the root view is not full screen, than instead of root view ,we need to get a reference to the main window as this will be our view against which we calculate coordinates now.
we have to
in our class where we calculate and then pass the window property as the reference view. It is guaranteed that window covers the screen and UIWindow is a subclass of UIView. So it just works.