What is a Window in an iOS application?

Ario Liyan
9 min readAug 8, 2023

--

In iOS app development, a window is a fundamental component of the user interface (UI) hierarchy. We want to talk about it generally in this post.

Link

Table of contents:

  • What is a window?
  • Window and Root ViewController
  • Window Hierarchy
  • The Key Window
  • Multiple Scenes and Windows
  • UIWindow Class
  • Summary

What is a window?

Link

In iOS app development, a window is a fundamental user interface (UI) hierarchy component. It represents a rectangular area on the screen that hosts and displays the app’s content. The window acts as a container for views and provides a backdrop on which the app’s user interface is presented.

Window and RootViewController

A window must have a root view controller associated with it. The root view controller manages the content that is displayed within the window. It coordinates the presentation and layout of views within the window’s bounds.


// APP Delegate
class AppDelegate: UIResponder, UIApplicationDelegate {
var window: UIWindow?

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
// Create a UIWindow instance
window = UIWindow(frame: UIScreen.main.bounds)

// Create an instance of your root view controller
let rootViewController = YourRootViewController()

// Assign the root view controller to the window
window?.rootViewController = rootViewController

// Make the window key and visible
window?.makeKeyAndVisible()

return true
}
}

// Scene Delegate
class SceneDelegate: UIResponder, UIWindowSceneDelegate {
var window: UIWindow?

func scene(_ scene: UIScene, willConnectTo session: UISceneSession, options connectionOptions: UIScene.ConnectionOptions) {
// Ensure the scene is of type UIWindowScene
guard let windowScene = scene as? UIWindowScene else { return }

// Create a UIWindow instance using the windowScene
window = UIWindow(windowScene: windowScene)

// Create an instance of your root view controller
let rootViewController = YourRootViewController()

// Assign the root view controller to the window
window?.rootViewController = rootViewController

// Make the window key and visible
window?.makeKeyAndVisible()
}
}

Window Hierarchy

In an iOS app, multiple windows can exist simultaneously, each representing a distinct area of the app’s UI. Windows are organized in a hierarchical structure, with the key window being the top-level window that receives user input events. Other windows can overlay or be positioned below “the key window” based on their window level.

The Key window

The key window refers to the window that is currently active and receiving user input events. It is the top-level window in the window hierarchy and is typically the window that is visible and actively processing user interactions.

Some details about the key window

1. User Input Events: The key window is responsible for handling user input events such as touch gestures, keyboard input, and motion events. When a user interacts with the app, the key window receives and dispatches these events to the appropriate views and view controllers.

2. Top-Level Window: The key window is positioned at the top of the window hierarchy. The window hierarchy is a stacked arrangement of windows, where the key window sits above other windows. This stacking order determines which window receives user input and how windows are visually layered on the screen.

3. Window Level: Each window in iOS has a window level associated with it. The window level represents the priority or importance of the window in relation to other windows. The key window typically has a higher window level compared to other windows, ensuring that it is the primary window for user interactions.

4. Window Resigning and Becoming Key: The key window can change during the runtime of an app. When a window resigns its key status, another window in the hierarchy becomes the new key window. This can happen when the user switches between apps or when your app programmatically changes the key window.

5. UIApplication’s keyWindow Property: In iOS, the UIApplication class provides the keyWindow property to access the currently active key window. We can use this property to retrieve the key window instance if we need to perform any operations or modifications related to the key window.

Here’s an example of changing the key window in an iOS app

To change the key window in an iOS app, we can use the makeKeyAndVisible() method of the UIWindow class. This method allows us to set a specified window as the key window, which will become the window that receives user input events. Here's an example of how we can change the key window:

  1. Get a reference to the window we want to set as the key window. We can access the window through the UIApplication.shared.windows array or by using UIWindow's keyWindow property.
  2. Call the makeKeyAndVisible() method on the window we want to set as the key window. This will make the window visible and set it as the key window.
// Assuming you have a reference to the window you want to set as the key window
let newKeyWindow: UIWindow = ...

// Make the newKeyWindow the key window
newKeyWindow.makeKeyAndVisible()

Note that starting from iOS 13, the keyWindow property of UIApplication is deprecated. In that case, we can use the windowScene property of our desired window to set it as the key window. Here's an example:

// Assuming you have a reference to the window you want to set as the key window
let newKeyWindow: UIWindow = ...

if let windowScene = newKeyWindow.windowScene {
UIApplication.shared.connectedScenes.forEach { scene in
if let sceneDelegate = scene.delegate as? SceneDelegate {
sceneDelegate.window = newKeyWindow
newKeyWindow.makeKeyAndVisible()
}
}
}

Remember to handle changing the key window carefully, as it can impact the user experience and the expected behavior of your app.

Multiple Scenes and Windows

We can have multiple scenes, and each scene can have its own window. This allows for more flexible and multitasking-oriented UI designs. For example, on iPad, an app can support multiple instances of its UI side by side, with each instance represented by a separate scene and window.

Example of an app with multiple scenes and windows

Imagine you’re building a messaging app called “Chatify” that supports multiple conversations open in separate scenes and windows. Here’s how it could work:

  1. Launching the App:
    When the user launches the Chatify app, the initial scene and window are created. The initial scene could display a list of conversations or a login screen, depending on the user’s authentication status.
  2. Opening a Conversation:
    Once the user logs in or selects a conversation from the initial scene, a new scene is created with a dedicated window for that conversation. Opening a conversation in a new scene allows the user to view and interact with the conversation independently of other conversations.
  3. Multiple Conversations:
    Chatify supports multiple conversations running simultaneously. Each conversation is represented by a separate scene and window. The user can switch between conversations by selecting the corresponding scene or window.
  4. Scene-Level Interactions:
    Each conversation scene has its own lifecycle and user interface. Users can interact with individual conversation scenes independently, scrolling through messages, sending replies, or accessing conversation-specific features. Any changes made within a specific scene, such as message updates, are isolated to that scene.
  5. Window-Level Interactions:
    Windows provide a high-level view of conversations, allowing users to see multiple conversations side by side or in a multitasking environment. Users can drag, resize, or split windows to customize their workspace according to their preferences.
  6. Scene and Window Management:
    Chatify manages the scene and window instances to ensure a seamless user experience. When a conversation is opened, a new scene and window are created. If the user switches to a different conversation, the corresponding scene and window become active, while the previously active conversation scene may be paused or placed in a background state.
  7. State Preservation and Restoration:
    iOS provides built-in mechanisms for state preservation and restoration. Chatify can leverage these features to save and restore the state of individual conversations, including messages, scroll positions, and other relevant data. This ensures that when the user reopens a conversation, they can continue from where they left off.

By supporting multiple scenes and windows, Chatify offers a flexible and multitasking-friendly user interface. Users can engage in multiple conversations simultaneously, easily switch between them, and customize their workspace by arranging conversation windows according to their preferences.

UIWindow Class

The UIWindow class represents a window object. We typically create an instance of UIWindow in our app’s code and associate it with a view controller to set it as the root view controller. UIWindow provides methods and properties to manage the window’s behavior, appearance, and interactions.

Responsibilities

  1. Displaying Content:
    A window is responsible for displaying the content of the app’s user interface. It acts as a container for views and manages the visual hierarchy of the app’s UI elements. Views are added as subviews to the window, and the window is responsible for rendering and displaying them on the screen.
  2. Managing the Root View Controller:
    A window has a root view controller associated with it. The root view controller represents the initial view controller that is displayed when the app launches. The window is responsible for managing the presentation and lifecycle of the root view controller, including displaying its view and handling transitions between different view controllers.
    When you assign a view controller to this property(Root View Controller), either programmatically or using Interface Builder, the view controller’s view is installed as the content view of the window. If the window has an existing view hierarchy, the old views are removed before the new ones are installed.
  3. Handling User Interactions:
    The window is responsible for handling user interactions within its bounds. It receives touch events, gestures, and other input events from the underlying system and dispatches them to the appropriate views and view controllers in the view hierarchy. The window ensures that the user interactions are delivered to the correct responder objects for processing.
  4. Managing the Window Level:
    Each window has a window level that determines its stacking order relative to other windows on the screen. The window level defines the visibility and layering of the window. The window with the highest window level appears in front of other windows with lower levels. The window is responsible for managing its window level to control its visibility and relationship to other windows.
  5. Coordinating Keyboard Input:
    The window plays a crucial role in coordinating keyboard input. It manages the keyboard appearance and handles keyboard events, such as showing and dismissing the keyboard, adjusting the view layout to accommodate the keyboard, and forwarding keyboard events to the appropriate views and view controllers.
  6. Handling Device Orientation and Size Changes:
    The window is responsible for handling device orientation changes and adapting the app’s user interface accordingly. It receives notifications about device rotation or size changes and notifies the appropriate view controllers in the view hierarchy to adjust their layout and update the UI for the new orientation or size.
  7. Coordinate with Scene and App Lifecycle:
    The window participates in the app and scene lifecycle. It receives notifications about app state changes, such as app activation, deactivation, backgrounding, or termination. The window can respond to these events and coordinate with other app components, such as the app delegate and scene delegate, to manage the app’s behavior during different lifecycle stages.

Overall, a window in iOS app development is responsible for managing the visual presentation, handling user interactions, coordinating with the view hierarchy, and participating in the app and scene lifecycle. It serves as a critical component for displaying your app’s user interface and managing the overall user experience.

UIWindowSceneDelegate

The UIWindowSceneDelegate, a protocol introduced with the advent of scenes, works in conjunction with the SceneDelegate. It provides methods to manage and configure windows specific to a scene. The UIWindowSceneDelegate is responsible for creating and configuring the window associated with a scene, responding to window-level events, and handling any relevant window-specific tasks.

It works in tandem with the SceneDelegate to handle window-related tasks within a scene. The UIWindowSceneDelegate provides methods such as scene(_:willConnectTo:options:), sceneDidBecomeActive(_:), and sceneWillResignActive(_:) to handle window-level events and perform necessary setup or teardown tasks.

Summary

In summary, a window in iOS represents a rectangular area on the screen that hosts and displays an app’s content. It provides a container for views and is associated with a root view controller. Multiple windows can exist in an app, each representing a distinct area of the UI. With the introduction of scenes, an app can have multiple scenes, each with its own window. UIWindow is the class that represents a window object and provides methods and properties for managing its behavior.

--

--

Ario Liyan

As an iOS developer with a passion for programming concepts. I love sharing my latest discoveries with others and sparking conversations about technology.