Add a UIView above all, even the navigation bar
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Introduction
In iOS development, UIView is a fundamental building block for creating user interfaces. By default, the view hierarchy in an iOS application consists of several layers, including the status bar, navigation bar, and view controllers. However, there might be scenarios where you want to overlay a UIView above everything else, including the navigation bar. This can be useful for creating custom UI elements such as a global notification banner, a pop-up alert, or any other overlay that needs to capture the user's attention.
In this article, we will explore the techniques required to add a UIView above all other views, including the navigation bar, and provide technical explanations and examples.
Understanding the View Hierarchy
Before diving into the implementation, it's essential to understand the typical view hierarchy in an iOS application:
- Window: The UIWindow object acts as the backdrop for your app's user interface and represents the main container for all your views.
- View Controllers: Manage the content of a single screen. Each view controller comes with its root view.
- Navigation Bar: An optional bar managed by a UINavigationController, typically positioned above the main content view.
- Status Bar: The area at the top of the screen displaying system status information.
When you add a UIView, it is generally bound within the confines of its parent view. Therefore, adding a view above the navigation bar can be challenging without understanding the appropriate approach.
Approach to Overlay UIView
To add a UIView above everything else, including the navigation bar, you should utilize the UIWindow. The UIWindow sits at the top of the view hierarchy and provides direct access to managing overlays across all application views.
Implementation Steps
- Access the Key Window: Start by getting a reference to the application's key window. With SwiftUI and SceneDelegate in iOS 13 and later, retrieving the window is slightly different. Here's how you can do it:
- Auto Layout Constraints: When adding the overlay directly to the window, you may opt to use Auto Layout constraints to handle screen rotations and varying screen sizes.
- Interactions: Overlaid views may intercept touch events meant for views below. Use `isUserInteractionEnabled` and gesture recognizers wisely.

