I’ve been meaning to look at this since 2013. Easy to get a second screen. Next, interacting with it.
References:
Understanding Windows and Screens
UIWindow
UIScreen
Presenting Content on an External Display
// ViewController.swift
import UIKit
class ViewController: UIViewController {
override func loadView() {
let overall = UIView(frame: UIScreen.main.bounds)
self.view = overall
}
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
}
}
// SecondViewController.swift
import UIKit
class ViewController: UIViewController {
override func loadView() {
let overall = UIView(frame: UIScreen.main.bounds)
self.view = overall
}
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
}
}
// AppDelegate.swift
import UIKit
import CoreData
@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {
var window: UIWindow?
var secondWindow: UIWindow?
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
// Override point for customization after application launch.
// Understanding Windows and ScreensIn rare cases you might want to create your app’s window programmatically https://developer.apple.com/library/content/documentation/WindowsViews/Conceptual/WindowAndScreenGuide/WindowScreenRolesinApp/WindowScreenRolesinApp.html#//apple_ref/doc/uid/TP40012555-CH4-SW1
window = UIWindow(frame: UIScreen.main.bounds)
let myViewController = ViewController()
window!.rootViewController = myViewController
window!.backgroundColor = UIColor.purple
window!.makeKeyAndVisible()
setUpScreenConnectionNotificationHandlers()
checkForExistingSecondScreen()
return true
}
// Listing 2-1 Checking for the presence of an external display https://developer.apple.com/library/content/documentation/WindowsViews/Conceptual/WindowAndScreenGuide/UsingExternalDisplay/UsingExternalDisplay.html#//apple_ref/doc/uid/TP40012555-CH3-SW3
func checkForExistingSecondScreen () {
if (UIScreen.screens.count > 1) {
// Get the screen object that represents the external display.
let secondScreen = UIScreen.screens[1]
// Get the screen's bounds so that you can create a window of the correct size.
let secondScreenBounds = secondScreen.bounds
secondWindow = UIWindow(frame: secondScreenBounds)
secondWindow!.screen = secondScreen
// Set up initial content to display...
let mySecondViewController = SecondViewController()
secondWindow!.rootViewController = mySecondViewController
secondWindow!.backgroundColor = UIColor.lightGray
// Show the window.
secondWindow!.isHidden = false
}
}
// Listing 2-2 Registering for screen connection and disconnection notifications https://developer.apple.com/library/content/documentation/WindowsViews/Conceptual/WindowAndScreenGuide/UsingExternalDisplay/UsingExternalDisplay.html#//apple_ref/doc/uid/TP40012555-CH3-SW6
func setUpScreenConnectionNotificationHandlers() {
let center = NotificationCenter.default
center.addObserver(self, selector: #selector(handleScreenDidConnect(notification:)), name: NSNotification.Name.UIScreenDidConnect, object: nil)
center.addObserver(self, selector: #selector(handleScreenDidDisconnect(notification:)), name: NSNotification.Name.UIScreenDidDisconnect, object: nil)
}
func handleScreenDidConnect(notification:NSNotification) {
let secondScreen = notification.object as! UIScreen
let secondScreenBounds = secondScreen.bounds
if (secondWindow == nil) {
secondWindow = UIWindow(frame: secondScreenBounds)
secondWindow!.screen = secondScreen
// Set up initial content to display...
let mySecondViewController = SecondViewController()
secondWindow!.rootViewController = mySecondViewController
secondWindow!.backgroundColor = UIColor.darkGray
// Show the window.
secondWindow!.isHidden = false
}
}
func handleScreenDidDisconnect(notification:NSNotification) {
if (secondWindow != nil) {
// Hide and then delete the window.
secondWindow?.isHidden = true
secondWindow = nil
}
}
}
