Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/99.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
用于';屏幕';在iOS 13.0中被弃用_Ios_Swift_Ios13_Uiwindow_Uiwindowscene - Fatal编程技术网

用于';屏幕';在iOS 13.0中被弃用

用于';屏幕';在iOS 13.0中被弃用,ios,swift,ios13,uiwindow,uiwindowscene,Ios,Swift,Ios13,Uiwindow,Uiwindowscene,我试图按照本教程创建一个多屏幕应用程序: 不幸的是,在25:00-26:00分钟,我收到一个错误,我的外部屏幕保持黑色: [Assert] Error in UIKit client: -[UIWindow setScreen:] should not be called if the client adopts UIScene lifecycle. Call -[UIWindow setWindowScene:] instead. 我的代码是: 导入UIKit 类ViewControlle

我试图按照本教程创建一个多屏幕应用程序:

不幸的是,在25:00-26:00分钟,我收到一个错误,我的外部屏幕保持黑色:

[Assert] Error in UIKit client: -[UIWindow setScreen:] should not be called if the client adopts
UIScene lifecycle. Call -[UIWindow setWindowScene:] instead.
我的代码是:

导入UIKit
类ViewController:UIViewController{
@IBOutlet弱var textView:UITextView!
var additionalWindows=[UIWindow]()
重写func viewDidLoad(){
super.viewDidLoad()
NotificationCenter.default.addObserver(forName:UIScreen.didConnectNotification,对象:nil,队列:nil){
guard let self=self-else{return}
guard let newScreen=notification.object as?UIScreen else{return}
让screenDimensions=newScreen.bounds
让newWindow=UIWindow(框架:屏幕尺寸)
newWindow.screen=新闻屏幕
guard let vc=self.storyboard?.InstanceEviewController(标识符为:“PreviewViewController”)作为?PreviewViewController else{
fatalError(“找不到PreviewViewController”)
}
newWindow.rootViewController=vc
newWindow.ishiden=false
self.additionalWindows.append(新窗口)
}
}
}

我在
newWindow.screen=newScreen
中有一个弃用警报:
Setter for'screen'在iOS 13.0中被弃用了,但我找不到任何有用的东西,也找不到解决这个问题的方法。

请注意,您“应该”按照externalWindow.rootViewController实例化VC

在我的例子中,我使用外部显示来显示一个
自定义UIView()
,因此我使用一个空的
UIViewController()
,然后将我的视图添加到其中

private func setupExternalScreen(screen: UIScreen, retryUntilSet: Bool = true, retryTimesUntilDiscarded: Int = 0) {
    var matchingWindowScene: UIWindowScene? = nil
    let scenes = UIApplication.shared.connectedScenes
    for item in scenes {
        if let windowScene = item as? UIWindowScene {
            if (windowScene.screen == screen) {
                matchingWindowScene = windowScene
                break
            }
            }
    }
    if matchingWindowScene == nil {
        if true == retryUntilSet {
            DispatchQueue.main.asyncAfter(deadline: .now() + 1) {
                if retryTimesUntilDiscarded < 5 {
                        self.setupExternalScreen(screen:screen, retryUntilSet: false, retryTimesUntilDiscarded += 1)
                } else {
                    let alert = UIAlertController(title: "Not connected", message: "Reconnect the display and try again", preferredStyle: .alert)
                        let ok = UIAlertAction(title: "Ok", style: .default, handler: nil)
                        alert.addAction(ok)
                        self.present(alert, animated: true, completion: nil)
                }
            }
        }
        return
    }
    externalWindow = UIWindow(frame: screen.bounds)
    externalWindow.rootViewController = UIViewController()
    airplayView.frame = externalWindow.frame
    if !externalWindow.subviews.contains(airplayView) {
        externalWindow.rootViewController?.view.addSubview(airplayView)
        if let _ = view.window {
            view.window?.makeKey()
        }
    } else {
        airplayView.updateValues()
    }

    externalWindow.windowScene = matchingWindowScene
    externalWindowWindow.isHidden = false
}

干得好,苹果。他们不赞成使用命令,但他们的文档中仍然提到使用屏幕设置器[关于如何设置屏幕属性的任何进展?不,我的项目处于搁置状态…这里有一些相关信息,虽然是为SWIFTUI准备的。没有太多时间来检查它。如果您想查看,这里有链接[
lazy var externalWindow = UIWindow()