Using convertPoint to get the relative position inside a parent UIView
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Introduction
Every UIView has its own coordinate system. A point that is (10, 10) inside a child view is not automatically (10, 10) inside its parent, because the child may be offset, transformed, or nested several levels deep.
That is what convert(_:to:) and convert(_:from:) are for. They let you translate a point from one view's coordinate space into another view's coordinate space without manually adding frame offsets yourself.
Think In Coordinate Spaces
The important mental model is that the same physical screen location can have different coordinates depending on which view is measuring it. If you ask a subview for a touch location, that point is local to the subview unless you convert it.
For example, if a button is inside a container view and you want the button-relative point expressed in the container's coordinates, use conversion instead of hand-written geometry math.
Convert A Point From Child To Parent
In Swift, the child can convert one of its local points into the parent's coordinate system:
That is usually the most readable form when you already have the point in the child's local coordinates.
You can express the same conversion from the parent's perspective:
Both calls answer the same question. They just start from opposite ends of the conversion.
A Touch-Handling Example
Touch locations are a common real-world use case. Suppose a gesture recognizer is attached to a child view, but you need the touch location inside the parent:
This is much safer than manually adding child.frame.origin because conversion also handles transforms and deeper hierarchy relationships.
Window And Sibling Conversions
You are not limited to parent-child conversions. You can convert between sibling views, to the window, or from the window back into a view.
This is useful when you need to line up popovers, overlays, drag previews, or custom hit-testing logic across different parts of the view tree.
frame Math Is Not The Same Thing
Developers sometimes try to solve everything with frame.origin. That can appear to work in a simple hierarchy, but it becomes fragile as soon as transforms, scroll views, or nested containers are involved.
convert exists because UIKit already knows the full relationship between views. Let UIKit perform the coordinate conversion instead of reconstructing it manually.
Another related distinction is bounds versus frame. bounds describes the view's internal coordinate space, while frame describes the view's rectangle in its superview. convert bridges these spaces for you when all you really care about is "where is this point in that other view?"
Common Pitfalls
One common mistake is converting in the wrong direction, which gives a valid but unintended point. Another is using frame.origin math and forgetting that transforms or scroll offsets change the result. Developers also often ask for a touch location in one view and then use it directly in another view without conversion. Finally, converting to a parent only works if you actually mean the parent's coordinate space. If you need screen-level alignment, convert to the window instead.
Summary
- Each
UIViewhas its own coordinate system, so the same visual point can have different coordinates in different views. - Use
convert(_:to:)orconvert(_:from:)instead of manual frame math. - This is especially useful for touch handling, overlays, and nested view hierarchies.
- '
converthandles transforms and hierarchy relationships that manual offset math often misses.' - Be explicit about which coordinate space you want: child, parent, sibling, or window.

