ker*_*nic 6 ios swift ios13 swiftui
我对通过 Airplay 和 SwiftUI 实现外部显示器支持感到困惑。
在 SceneDelegate.swift 中,我正在使用UIScreen.didConnectNotification观察者,它实际上检测到一个新的屏幕被附加,但我无法将自定义 UIScene 分配给屏幕。
我发现了一些在 iOS12 及更低版本中使用 Swift 的好例子,但它们都不适用于 SwiftUI,因为整个范式已更改为使用 UIScene 而不是 UIScreen。这是清单:
https://www.swiftjectivec.com/supporting-external-displays/
也许有些事情发生了变化,现在有一种新方法可以正确地做到这一点。此外,设置UIWindow.screen = screen已在 iOS13 中弃用。
有没有人已经尝试过使用 SwiftUI 实现外部屏幕支持。任何帮助深表感谢。
小智 4
我修改了 Big Nerd Ranch 博客中的示例,使其工作如下。
删除主故事板:我从新项目中删除了主故事板。在部署信息下,我将主界面设置为空字符串。
编辑 plist:在 plist 的应用程序场景清单部分定义两个场景(默认和外部)及其场景委托。
<key>UIApplicationSceneManifest</key>
<dict>
<key>UIApplicationSupportsMultipleScenes</key>
<true/>
<key>UISceneConfigurations</key>
<dict>
<key>UIWindowSceneSessionRoleApplication</key>
<array>
<dict>
<key>UISceneConfigurationName</key>
<string>Default Configuration</string>
<key>UISceneDelegateClassName</key>
<string>$(PRODUCT_MODULE_NAME).SceneDelegate</string>
</dict>
</array>
<key>UIWindowSceneSessionRoleExternalDisplay</key>
<array>
<dict>
<key>UISceneDelegateClassName</key>
<string>$(PRODUCT_MODULE_NAME).ExtSceneDelegate</string>
<key>UISceneConfigurationName</key>
<string>External Configuration</string>
</dict>
</array>
</dict>
</dict>
Run Code Online (Sandbox Code Playgroud)
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
view.backgroundColor = .blue
view.addSubview(screenLabel)
}
var screenLabel: UILabel = {
let label = UILabel()
label.textColor = UIColor.white
label.font = UIFont(name: "Helvetica-Bold", size: 22)
return label
}()
override func viewDidLayoutSubviews() {
/* Set the frame when the layout is changed */
screenLabel.frame = CGRect(x: 0,
y: 0,
width: view.frame.width - 30,
height: 24)
}
}
Run Code Online (Sandbox Code Playgroud)
func scene(_ scene: UIScene, willConnectTo session: UISceneSession, options connectionOptions: UIScene.ConnectionOptions) {
// Use this method to optionally configure and attach the UIWindow `window` to the provided UIWindowScene `scene`.
// If using a storyboard, the `window` property will automatically be initialized and attached to the scene.
// This delegate does not imply the connecting scene or session are new (see `application:configurationForConnectingSceneSession` instead).
guard let windowScene = (scene as? UIWindowScene) else { return }
window = UIWindow(frame: windowScene.coordinateSpace.bounds)
window?.windowScene = windowScene
let vc = ViewController()
vc.loadViewIfNeeded()
vc.screenLabel.text = String(describing: window)
window?.rootViewController = vc
window?.makeKeyAndVisible()
window?.isHidden = false
}
Run Code Online (Sandbox Code Playgroud)
为您的外部屏幕创建一个场景委托。我创建了一个新的 Swift 文件 ExtSceneDelegate.swift,其中包含与 SceneDelegate.swift 相同的文本,并将类的名称从 SceneDelegate 更改为 ExtSceneDelegate。
修改 AppDelegate 中的 application(_:configurationForConnecting:options:) 。其他人建议,如果您将其注释掉,一切都会好起来的。为了调试,我发现将其更改为以下内容很有帮助:
func application(_ application: UIApplication, configurationForConnecting connectingSceneSession: UISceneSession, options: UIScene.ConnectionOptions) -> UISceneConfiguration {
// This is not necessary; however, I found it useful for debugging
switch connectingSceneSession.role.rawValue {
case "UIWindowSceneSessionRoleApplication":
return UISceneConfiguration(name: "Default Configuration", sessionRole: connectingSceneSession.role)
case "UIWindowSceneSessionRoleExternalDisplay":
return UISceneConfiguration(name: "External Configuration", sessionRole: connectingSceneSession.role)
default:
fatalError("Unknown Configuration \(connectingSceneSession.role.rawValue)")
}
}
Run Code Online (Sandbox Code Playgroud)
对我来说,解决所有这些问题的关键参考是https://onmyway133.github.io/blog/How-to-use-external-display-in-iOS/。
| 归档时间: |
|
| 查看次数: |
1408 次 |
| 最近记录: |