iOS 13.0中不推荐使用“屏幕"设置程序 [英] Setter for 'screen' was deprecated in iOS 13.0

查看:121
本文介绍了iOS 13.0中不推荐使用“屏幕"设置程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I was trying to follow this tutorial to create a multi screen application:

https://www.youtube.com/watch?v=UYviLiI2rlY&; t = 774s

不幸的是,在25:00-26:00时,我收到错误消息,并且外部屏幕保持黑屏:

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.

我的代码是:

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)
        }
    }


}

我在 newWindow.screen = newScreen 中有一个弃用警报:'screen'的设置在iOS 13.0中已弃用,但我找不到有用的东西,没有解决该问题的方法过于复杂.

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.

推荐答案

请注意,您应该按照 externalWindow.rootViewController

在我的情况下,我使用外部显示器来呈现自定义UIView(),因此我使用了空的 UIViewController(),然后将其添加到视图中./p>

In my case, I used the external display for presenting a custom UIView() so I use an empty UIViewController() and then add my view to it.

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
}

如果您的应用需要iOS< 13,则可能需要使用如果#available(iOS 13.0,*){} 决定如何设置您的外部屏幕.

If your app requires iOS<13 you might need to use if #available(iOS 13.0, *) {} to decide how to setup your external screen.

忘记提及... externalWindow 在我需要使用第二个屏幕的ViewController中声明

Forgot to mention... externalWindow is declared inside the ViewController where I demand using the second screen

lazy var externalWindow = UIWindow()

这篇关于iOS 13.0中不推荐使用“屏幕"设置程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆