I was trying to follow this tutorial to create a multi screen application:
https://www.youtube.com/watch?v=UYviLiI2rlY&t=774s
Unfortunately at min 25:00 - 26:00 I receive an error and my external screen stay black:
[Assert] Error in UIKit client: -[UIWindow setScreen:] should not be called if the client adopts
UIScene lifecycle. Call -[UIWindow setWindowScene:] instead.
My code is:
import UIKit
class ViewController: UIViewController {
@IBOutlet weak var textView: UITextView!
var additionalWindows = [UIWindow]()
override func viewDidLoad() {
super.viewDidLoad()
NotificationCenter.default.addObserver(forName: UIScreen.didConnectNotification, object: nil, queue: nil) { [weak self] notification in
guard let self = self else {return}
guard let newScreen = notification.object as? UIScreen else {return}
let screenDimensions = newScreen.bounds
let newWindow = UIWindow(frame: screenDimensions)
newWindow.screen = newScreen
guard let vc = self.storyboard?.instantiateViewController(withIdentifier: "PreviewViewController") as? PreviewViewController else {
fatalError("Unable to find PreviewViewController")
}
newWindow.rootViewController = vc
newWindow.isHidden = false
self.additionalWindows.append(newWindow)
}
}
}
And I have a deprecation alert in newWindow.screen = newScreen : Setter for 'screen' was deprecated in iOS 13.0 but I can't find anything useful and not overcomplicated on how to solve this issue.
Note that you 'should' instantiate a VC as per the externalWindow.rootViewController
In my case, I used the external display for presenting a
custom UIView()so I use an emptyUIViewController()and then add my view to it.If your app requires iOS<13 you might need to use
if #available(iOS 13.0, *) {}to decide how to setup your external screen.Forgot to mention...
externalWindowis declared inside the ViewController where I demand using the second screen