Get position of UIView in respect to its superview's superview
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Introduction
In UIKit, every UIView frame is expressed in its own superview coordinate space. Because of that, comparing coordinates across hierarchy levels without conversion leads to wrong positions. The correct solution is to use UIKit conversion methods to translate points or rectangles between view spaces, especially once Auto Layout, scrolling, or transforms are involved.
Coordinate Spaces in UIKit
A child view does not know absolute screen coordinates directly. It only knows values relative to its immediate parent. If you need position relative to grandparent or root view, convert coordinates through UIKit APIs.
Key methods:
convert(_:to:)convert(_:from:)
These methods account for transforms, scroll offsets, and hierarchy structure better than manual frame addition.
Convert Child Frame to Grandparent Space
Suppose you need the child frame relative to its superview parent superview.
This returns the same rectangle expressed in grandparent coordinates.
Another common pattern is converting child.bounds from the child itself rather than converting child.frame from the parent. Both can work, but converting bounds often makes the intent clearer because the source rectangle is explicitly defined in the child coordinate system.
Convert Points for Gesture and Hit Testing
For touch handling, point conversion is often more useful than full frame conversion.
Use this for custom gesture routing, overlays, and alignment between unrelated sibling trees.
Timing Matters with Auto Layout
Coordinate queries can be wrong if called before layout has finalized frames. Call conversion code after layout pass, such as in:
viewDidLayoutSubviewslayoutSubviews- after
layoutIfNeeded
Example:
Running conversion in viewDidLoad often returns stale values.
Scroll Views and Transforms
UIScrollView changes visible positioning via content offset. Conversion APIs handle this automatically, while manual arithmetic often misses it. Similarly, rotated or scaled views are represented correctly by conversion methods.
If visual result still looks wrong, inspect:
- active transforms.
- safe area insets.
- whether reference view is the expected ancestor.
Window and Screen Coordinates
To place overlays across the full interface, convert through window coordinates.
This is useful for popovers, tutorials, and debug overlays that need global placement.
Debugging Coordinate Issues
When positions look off, print a coordinate trace at each level:
- Child frame in parent.
- Parent frame in ancestor.
- Converted frame result.
Also use Xcode view debugger to verify hierarchy assumptions. Many coordinate bugs are actually hierarchy misunderstandings after runtime view reparenting.
Convert Between Sibling Views
A frequent task is positioning one view relative to a sibling from another branch in the hierarchy. Convert through a common ancestor or window.
This pattern is useful for tooltip alignment, drag previews, and temporary overlay animations.
When results still look slightly off, verify whether the visual element has extra padding or internal content insets. Sometimes the conversion is correct, but the frame you chose does not match the visually relevant subregion.
Common Pitfalls
- Manually summing frame origins instead of using conversion APIs.
- Querying positions before Auto Layout has finalized frames.
- Ignoring scroll view content offset effects on visible position.
- Converting to the wrong destination view in complex hierarchies.
- Assuming window coordinates exist before a view is attached to window.
Summary
- UIKit coordinates are local to each superview, not global.
- Use
convertmethods for safe cross hierarchy coordinate mapping. - Run conversion after layout to avoid stale frame values.
- Prefer point conversion for touch workflows and frame conversion for layout alignment.
- Validate hierarchy assumptions when debugging position mismatches.

