Understand convertRecttoView, convertRectFromView, convertPointtoView and convertPointfromView methods
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Introduction
UIKit views each have their own coordinate system. A point that means "10 points from the left edge" inside one view does not mean the same thing inside its parent, sibling, or window, which is why UIKit provides conversion methods for moving points and rectangles between view coordinate spaces.
The four classic Objective-C methods are convertRect:toView:, convertRect:fromView:, convertPoint:toView:, and convertPoint:fromView:. In Swift, you usually call the modern equivalents convert(_:to:) and convert(_:from:).
Start with Coordinate Spaces
Every view has a local origin at its own top-left corner. If a subview sits at x = 50, y = 100 inside its parent, a point inside that subview must be translated before the parent can understand where that point is.
That is what conversion does:
- point conversion translates a single location
- rect conversion translates both origin and size, accounting for transforms
This becomes essential for hit testing, animations, popovers, tooltips, and aligning overlays.
Convert a Point to Another View
Suppose you have a button inside a container, and you want the button's top-left corner expressed in the root view's coordinates:
The point starts in the button's local coordinate system and ends in the root view's coordinate system.
If you wanted the opposite direction, you would use rootView.convert(_:to:) or button.convert(_:from:) depending on which source space you are starting from.
Convert a Rectangle
Rect conversion is common when you need a whole frame-like area in another coordinate space:
This is often cleaner than manually summing frame origins through the hierarchy, and it remains correct if intermediate views apply transforms.
to Versus from
These method names are easier if you read them literally:
- '
convert(_:to:)takes coordinates currently expressed in the receiver's space and converts them into another view's space' - '
convert(_:from:)takes coordinates from another view's space and converts them into the receiver's space'
Example:
That works because the point 30, 40 in the container corresponds to the button's origin.
What nil Means
Passing nil instead of a view converts to or from the window base coordinate system. That is useful when positioning overlays relative to the whole screen region managed by the current window.
This is especially useful for floating UI such as contextual menus and debugging overlays.
Why Manual Math Is Risky
Developers sometimes try to compute positions by adding frame.origin values up the view hierarchy. That works only for simple layouts with no transforms, scrolling offsets, or special container behavior.
convert is safer because UIKit already knows about:
- nested view hierarchies
- transforms
- coordinate flips and bounds offsets
- scroll view content positioning
If UIKit already has a tested coordinate conversion API, use it.
Common Pitfalls
- Confusing
framewithbounds.boundsis local to the view;frameis expressed in the superview's coordinates. - Reversing
toandfrom, which yields correct-looking but wrong coordinates. - Forgetting that passing
nilmeans window coordinates, not "do nothing." - Replacing
convertwith manual origin arithmetic and then getting wrong results when transforms or scrolling are involved.
Summary
- Each
UIViewhas its own coordinate system, so points and rects often need conversion. - Use
convert(_:to:)when the source coordinates are in the receiver's space. - Use
convert(_:from:)when the source coordinates come from another view's space. - Convert rects when you need full areas, not just single locations.
- Prefer UIKit conversion APIs over manual coordinate math because they handle hierarchy and transforms correctly.

