iOS13外部屏幕镜像,出现错误:不应调用setScreen [英] iOS13 external screen mirroring, getting error: setScreen should not be called
问题描述
我正在尝试向iOS 13中的外部连接屏幕呈现自定义视图控制器.我遵循了本教程:
I am trying to present a custom view controller to external connected screen in iOS 13. I followed this tutorial:
我有以下代码:
/// A private method used to setup a external screen with a window
/// loaded with a ExternalScreenViewController
///
/// - parameter screen: A UIScreen object to connect the
/// ExternalScreenViewController too
private func setupExternalScreen(screen: UIScreen) {
guard externalWindow == nil,
let vc = self.storyboard?.instantiateViewControllerWithIdentifier("ExternalScreen") as? ExternalScreenViewController else {
return
}
externalWindow = UIWindow(frame: screen.bounds)
externalWindow!.rootViewController = vc
externalWindow!.screen = screen
externalWindow!.hidden = false
}
在 externalWindow!.screen = screen
行中,我在Xcode 11.4.1控制台中遇到错误:
In the line externalWindow!.screen = screen
I am getting an error in the Xcode 11.4.1 console:
[声明] UIKit客户端中的错误:-如果客户端采用UIScene生命周期,则不应调用-[UIWindow setScreen:].呼叫-[UIWindow setWindowScene:].
自定义视图控制器未显示在外部显示器中.
The custom view controller does not show up in the external display.
我应该改变什么?我想同时处理iOS 13.x和以前的设备.
What should I change? I want to handle both iOS 13.x and prior devices.
推荐答案
在iOS 13设备中,应在上设置
对象.在此之前,您应该从 windowScene
,而不是设置 screen
externalWindow UIApplication
的连接场景中找到匹配的 windowScene
.修改后的方法如下:
In iOS 13 devices, instead of setting screen
, you should set windowScene
on the externalWindow
object. Before that, you should find out matching windowScene
from UIApplication
's connected scenes. The revised method is as follows:
private func setupExternalScreen(screen: UIScreen, shouldRecurse: Bool = true) {
// For iOS13 find matching UIWindowScene
var matchingWindowScene: UIWindowScene? = nil
if #available(iOS 13.0, *) {
let scenes = UIApplication.shared.connectedScenes
for aScene in scenes {
if let windowScene = aScene as? UIWindowScene {
// Look for UIWindowScene that has matching screen
if (windowScene.screen == screen) {
matchingWindowScene = windowScene
break
}
}
}
if matchingWindowScene == nil {
// UIWindowScene has not been created by iOS rendered yet
// Lets recall self after delay of two seconds
if true == shouldRecurse {
DispatchQueue.main.asyncAfter(deadline: DispatchTime.now() + 2) {
self.setupExternalScreen(screen:screen, shouldRecurse: false)
}
}
// Dont proceed furthure in iOS13
return
}
}
guard externalWindow == nil, let vc = self.storyboard?.instantiateViewControllerWithIdentifier("ExternalScreen") as? ExternalScreenViewController else {
return
}
externalWindow = UIWindow(frame: screen.bounds)
externalWindow!.rootViewController = vc
if #available(iOS 13.0, *) {
// Set windowScene here, no need to set screen
externalWindow!.windowScene = matchingWindowScene
} else {
// Set screen the traditional way
externalWindow!.screen = screen
}
externalWindow!.isHidden = false
}
注意:我们应该将 isHidden
设置为不建议使用 hidden
.
Note: we should set isHidden
as hidden
is deprecated.
这篇关于iOS13外部屏幕镜像,出现错误:不应调用setScreen的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!