1

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

在我的 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
        }
    }

什么替换了这个设置器,我需要如何更新代码才能让它工作?

4

0 回答 0