iOS 13 后的外部显示器

问题描述

在 iOS 13 中,screen 的 setter 已被弃用,并且没有太多文档说明替代方案应该是什么。我查看了其他堆栈溢出帖子,例如 this one,但代码在我的应用中不起作用。这是我在视图控制器中设置外部显示器的功能,该功能有效,但警告我 setter 已被弃用用于屏幕。它在我的 ViewController 的 viewDidLoad() 函数调用

在我的 ViewController 中初始化的变量

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

连接和显示到外部屏幕的功能

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

什么取代了这个 setter,我需要如何更新代码才能使其工作?

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)