Getting reference to the top-most view/window in iOS application
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.
Introduction
In older iOS code, developers often reached for UIApplication.shared.keyWindow and assumed there was only one active window. That is no longer a safe assumption once scenes were introduced, so the modern answer is to start from the foreground UIWindowScene and then walk to the visible view controller.
Start From the Active Window Scene
If your app supports iOS 13 or later, the application can own multiple scenes. That means "top-most window" is only meaningful inside the currently active scene.
This helper does two important things:
- it ignores background scenes
- it returns the key window for the foreground scene instead of a random window
That is the correct starting point for presenting alerts, debugging view hierarchy issues, or attaching an overlay owned by the active interface.
Walk to the Visible View Controller
The top-most visible controller is often not the root controller. A tab bar controller can contain a navigation controller, which can present a sheet, which can present another controller. You have to follow the chain.
You can combine both helpers like this:
That function is often enough for diagnostic tools and app-wide overlays.
Presenting Something Safely
A common reason to ask for the top-most controller is to present an alert from shared infrastructure. The helper below avoids presenting from a hidden controller.
This works, but it should still be treated as a fallback. In feature code, the better design is usually to pass the presenting controller explicitly rather than searching for one globally.
When You Need the Window Instead of the Controller
Sometimes you really need the window, for example when adding a debug border, a floating performance meter, or a touch indicator view. In those cases, use the window from the active scene and add your subview there.
Be careful with multiple windows inside a scene. Keyboard windows, external display windows, or framework-owned windows may exist. The key window is usually the correct user-facing one, but if your app creates custom windows, you may need a stronger selection rule based on window level or ownership.
Common Pitfalls
- Using deprecated single-window assumptions in a scene-based app.
- Presenting from
rootViewControllereven when another controller is already on screen. - Treating a global top-most controller helper as the primary architecture instead of a fallback.
- Forgetting that background scenes can still exist and should be ignored.
- Adding overlays to the wrong window when the app manages more than one window.
Summary
- In modern iOS, start from the active
UIWindowScene, not from an old global window API. - Find the key window for the foreground scene, then walk the controller hierarchy.
- A recursive helper can handle navigation, tab, and presented controllers cleanly.
- Presenting from the discovered top controller is useful for shared infrastructure and debugging.
- When possible, pass the correct presenter explicitly instead of relying on a global lookup.
Related reading
- Getting row of UITableView cell on button press
- Getting the current Fragment instance in the viewpager
- Getting the difference between two Dates months/days/hours/minutes/seconds in Swift
- Getting the difference between two Dates months/days/hours/minutes/seconds in Swift
- getting the screen density programmatically in android?
- Getting the Value of a UITextField as keystrokes are entered?
- Getting version and build information with Swift
- Getting version and build information with Swift
.png&w=3840&q=75)
Tackling System Design Interview Problems
A short course that equips you with the skills to approach system design interviews methodically.
Start the free courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.