UIView
convertPoint
iOS development
programming
Swift

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:

swift
1import UIKit
2
3let localPoint = CGPoint(x: 20, y: 15)
4let pointInParent = childView.convert(localPoint, to: parentView)
5print(pointInParent)

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:

swift
let pointInParent = parentView.convert(localPoint, from: childView)

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:

swift
1@objc func handleTap(_ gesture: UITapGestureRecognizer) {
2    guard let child = gesture.view,
3          let parent = child.superview else { return }
4
5    let localPoint = gesture.location(in: child)
6    let parentPoint = child.convert(localPoint, to: parent)
7
8    print("Local:", localPoint)
9    print("In parent:", parentPoint)
10}

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.

swift
1if let window = view.window {
2    let pointInWindow = childView.convert(CGPoint(x: 0, y: 0), to: window)
3    let pointInOtherView = otherView.convert(pointInWindow, from: window)
4    print(pointInOtherView)
5}

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 UIView has its own coordinate system, so the same visual point can have different coordinates in different views.
  • Use convert(_:to:) or convert(_:from:) instead of manual frame math.
  • This is especially useful for touch handling, overlays, and nested view hierarchies.
  • 'convert handles transforms and hierarchy relationships that manual offset math often misses.'
  • Be explicit about which coordinate space you want: child, parent, sibling, or window.

Course illustration
Course illustration

All Rights Reserved.