UIView. How Do I Find the Root SuperView Fast?

dugla picture dugla · May 29, 2011 · Viewed 15.5k times · Source

I have a random child view in a view hierarchy. What is the best/fastest/cleverest way to get to the root superview?

Cheers,
Doug

Answer

Scott Forbes picture Scott Forbes · May 29, 2011

If your app only has one UIWindow (usually true), and if that window's only subview is your root controller's view (also usually true):

[randomChildView.window.subviews objectAtIndex:0]

Or you could recursively climb out of the hierarchy, by checking view.superview until you find a view whose superview is nil.