Supporting External Displays

The WWDC 2018 video Adding Delight to your iOS App shows how to support external displays.

Test to see if there is more than one screen available.

UIScreen.screens.count > 1

Observe screen connect or disconnect notifications.

    let notificationCenter = NotificationCenter.default

    notificationCenter.addObserver(self,
       selector: #selector(connectScreenHandler),
       name: UIScreen.didConnectNotification,
       object: nil)     
    notificationCenter.addObserver(self,
        selector: #selector(disconnectScreenHandler),
        name: UIScreen.didDisconnectNotification,
        object: nil)

Add the external screen and window. Configure the window then make it visible.

    if let externalScreen = UIScreen.screens.last {
        externalWindow = UIWindow()

        if let thisWindow = externalWindow {
            thisWindow.screen = externalScreen
            configureExternalWindow()
            thisWindow.isHidden = false
        }
    }

Remove the window and free up resources when disconnecting from the external display.

    externalWindow?.isHidden = true
    externalWindow = nil

Create a custom UI for the external display.

    // create a custom UI for the external display
    externalViewController = UIViewController()

    externalViewController!.view.bounds = windowBounds
    externalViewController!.view.frame = windowFrame

    externalViewController!.view.backgroundColor = UIColor.blue

    thisWindow.addSubview(externalViewController!.view)

Build and Run. Launch the Simulator. Select Hardware, External Displays, and then a display option. You should see a custom ‘blue’ view controller show up on the external display.

Your custom UI should present ‘public’ information without any UI outlets or controls. You can create a custom view controller with an image view and present the selected image on the external display.

    externalViewController?.imageView.image = image

Download the sample project.

Leave a comment

Your email address will not be published. Required fields are marked *