UIWindow

A window is an instance of the UIWindow class and it is the topmost element of any application UI’s hierarchy. It doesn’t draw any visual object and can be considered as a blank container for the UI elements called views. An application must have at least one window that normally fills the entire screen.

One of the main roles of the window is to deliver touches to the underlying views. A window is the first entry point for a touch event. The touch is then pushed down through the view hierarchy until it reaches the right view.

The contents of your application are mainly directed by view controllers and presented through views, which are displayed inside a window. The easiest and most reliable way to send content to a window is by configuring its rootViewController property with a UIViewController instance. The view controller’s view will automatically be set as the contents of the window and presented to the user.

The view controller is initialised and set as the root view controller of the window. Finally, the window presents the current root view controller’s view.

Configuring windows

When you create a new Xcode project using the wizard, a Storyboard is created for you. If you check the Info.plist file, you’ll see that the Main storyboard filebase name key reports the name of the Storyboard by default as Main.

This key tells Xcode that you want to start the application from the Storyboard initial view controller (the one indicated by the grey arrow inside the Storyboard working area).

The @UIApplication attribute in the AppDelegate.swift file is responsible for the launch of the entire application process. It marks an entry point for the application launch, reading the Storyboard’s information from the Info.plist file and instantiating the initial view controller.

At this point, a UIWindow instance is created and associated with the window property of the AppDelegate class. The initial view controller, previously initialised, is now assigned to the rootViewController property of the window; therefore, the initial view controller’s view becomes the current window’s contents.

Since windows are invisible by default, there is one last step required to show the content to the user. After the application:didFinishLaunchingWithOptions function finishes its execution, makeKeyAndVisible is called for the window, which now loads its interface from rootViewController and finally displays its contents.

The same results can be obtained programmatically.

If you remove the Main storyboard filebase name key from the Info.plist file, Xcode won’t have any information on how to set up a valid window. The application:didFinishLaunchingWithOptions is the place to do it.

func application(_ application: UIApplication,
    didFinishLaunchingWithOptions launchOptions:
    [UIApplication.LaunchOptionsKey: Any]?) -> Bool 
{
    // Instantiate a window with the same size as a screen
    window = UIWindow(frame: UIScreen.main.bounds)

    // Instantiate a view controller with the Main storyboard
    let storyboard = UIStoryboard(name: "Main", bundle: nil)
    let viewController = storyboard
        .instantiateViewController(withIdentifier: "ViewController") 
        as! ViewController

    window?.rootViewController = viewController
    window?.makeKeyAndVisible()

    return true
}

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s