> let convertToWindow = subview.convert(subview.frame, to: window)

Are you sure that’s correct? The documentation says “Converts a rectangle from the receiver’s coordinate system to that of another view.”

The receiver is subview but the rectangle you are passing is the subview’s frame, which is in the coordinate system of its superview (`view`). This is the classic frame/bounds confusion.

I think these will do the right thing (the first one relies on knowing that view is subview's superview, the other just uses the superview):

let convertToWindow = view.convert(subview.frame, to: window)

let convertToWindow = subview.superview.convert(subview.frame, to: window)

Thinking about it differently, rootView is at the origin of the window, so will have no effect. view is at 25,25 and subview is at 25,25 within view. So the origin of subview in window's coordinate system will be 50,50 and not 75,75 as your article (currently) states.

As an aside, I prefer to use the “from” rather than “to” versions. It helps me think in the coordinate system of where I’m converting *to*, rather than where I’m coming *from*. They are equivalent, but this way the message is sent to the view that the rect will be used with.

let convertToWindow = window.convert(subview.frame, from: view)

let convertToWindow = window.convert(subview.frame, from: subview.superview)

I *think* my examples are correct. Apologies if I got it wrong! ;-)

Written by

Independent and freelance software developer for iPhone, iPad and Mac

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store