External Displays Post iOS 13

53 Views Asked by At

With iOS 13 the setter for screen was deprecated, and there's not much documentation out there that says what the alternative should be. I have looked at other stack overflow posts such as this one but the code does not function in my app. This is the function I have in my view controller to set up external displays, which works, but warns me that the setter has been deprecated for screen. It is called in the viewDidLoad() function of my ViewController.

Variables initialized in my ViewController

    // External Display Support
    var secondWindow: UIWindow?
    var secondScreenView: UIView?

Function to connect and display to external screen.

    @objc func setupScreen() {
        if UIScreen.screens.count > 1 {
            // Find and store the second screen
            let secondScreen = UIScreen.screens[1]
            
            // Create a local variable to store the second window using the screen's dimensions
            let externalWindow = UIWindow(frame: secondScreen.bounds)
            
            // Windows require a root view controller
            let viewController = UIViewController()
            
            // Tell the window which screen to use
            externalWindow.screen = secondScreen // This is where the deprecation error is
            
            // Set the dimensions for the view for the external screen so it fills the screen
            secondScreenView = UIView(frame: externalWindow.frame)
            
            // Add the view to the window
            externalWindow.addSubview(secondScreenView)
            
            // Unhinde the window
            externalWindow.isHidden = false
            
            // Configure the View
            let hostingController = HostingControllerPresentable(rootView: DefaultPresentationView(appIcon: Bundle.main.icon ?? UIImage()))
            
            viewController.addChild(hostingController)
            viewController.view.addSubview(hostingController.view)
            hostingController.view.translatesAutoresizingMaskIntoConstraints = false
            
            NSLayoutConstraint.activate([
                hostingController.view.topAnchor.constraint(equalTo: (viewController.view.topAnchor)),
                hostingController.view.bottomAnchor.constraint(equalTo: (viewController.view.bottomAnchor)),
                hostingController.view.leadingAnchor.constraint(equalTo: (viewController.view.leadingAnchor)),
                hostingController.view.trailingAnchor.constraint(equalTo: (viewController.view.trailingAnchor)),
            ])
            
            hostingController.didMove(toParent: externalWindow.rootViewController)
            secondWindow = externalWindow
            externalWindow.rootViewController = viewController
        }
    }

What replaces this setter, and how do I need to update the code for this to work?

0

There are 0 best solutions below