在Mac Catalyst中打开新窗口

问题描述 投票:2回答:1

我正在使用Mac Catalyst移植iPad应用程序。我正在尝试在新窗口中打开视图控制器。

如果我严格使用AppKit,则可以执行此post中所述的操作。但是,由于我使用的是UIKit,因此没有可用的showWindow()方法。

This article指出,可以通过在项目的新捆绑包中添加AppKit来实现这一点(我这样做了),但是并未解释如何实际呈现新窗口的细节。它显示为...

您不能完全做的另一件事是使用UIKit视图层次结构生成一个新的NSWindow但是,您的UIKit代码具有生成新窗口场景的能力,而您的AppKit代码具有获取呈现在其中的结果NSWindow并劫持它以对其进行处理的能力,因此从这种意义上讲,您可以生成用于辅助调色板和其他各种功能的UIKit窗口。

任何人都知道如何实现本文中介绍的内容吗?

TL; DR:如何使用Mac Catalyst将UIViewController作为新的单独NSWindow打开?

ios macos uikit appkit maccatalyst
1个回答
4
投票

编辑:有关如何使用其他不同窗口的附加信息

为了在Mac上支持多个窗口,您需要做的就是在iPad上支持多个窗口。

您可以在22:28分钟的this WWDC会话中找到所需的所有信息,但总结起来,需要做的就是支持新的Scene生命周期模型。

首先编辑目标并选中支持多个窗口的选中标记

enter image description here

一旦完成,请单击配置选项,该选项会将您带到info.plist。确保您具有“应用程序场景清单”的正确条目

enter image description here

创建一个名为SceneDelegate.swift的新swift文件,然后将以下样板代码粘贴到其中

import UIKit

class SceneDelegate: UIResponder, UIWindowSceneDelegate {

    var window: UIWindow?


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

        // Create the SwiftUI view that provides the window contents.
       guard let _ = (scene as? UIWindowScene) else { return }
    }

    func sceneDidDisconnect(_ scene: UIScene) {
        // Called as the scene is being released by the system.
        // This occurs shortly after the scene enters the background, or when its session is discarded.
        // Release any resources associated with this scene that can be re-created the next time the scene connects.
        // The scene may re-connect later, as its session was not neccessarily discarded (see `application:didDiscardSceneSessions` instead).
    }

    func sceneDidBecomeActive(_ scene: UIScene) {
        // Called when the scene has moved from an inactive state to an active state.
        // Use this method to restart any tasks that were paused (or not yet started) when the scene was inactive.
    }

    func sceneWillResignActive(_ scene: UIScene) {
        // Called when the scene will move from an active state to an inactive state.
        // This may occur due to temporary interruptions (ex. an incoming phone call).
    }

    func sceneWillEnterForeground(_ scene: UIScene) {
        // Called as the scene transitions from the background to the foreground.
        // Use this method to undo the changes made on entering the background.
    }

    func sceneDidEnterBackground(_ scene: UIScene) {
        // Called as the scene transitions from the foreground to the background.
        // Use this method to save data, release shared resources, and store enough scene-specific state information
        // to restore the scene back to its current state.
    }


}

您基本上完成了。运行您的应用程序,然后按Command + N创建所需数量的新窗口。

如果要在代码中创建新窗口,则可以使用此方法:

@IBAction func newWindow(_ sender: Any) {            
    UIApplication.shared.requestSceneSessionActivation(nil, userActivity: nil, options: nil) { (error) in
        //
    }
}

现在我们已经知道了如何创建其他窗口的大谜团

关键是在应用程序中创建多种场景类型。您可以在无法正常工作的info.plist中或AppDelegate中进行操作。

让功能更改为创建新窗口:

@IBAction func newWindow(_ sender: Any) {     
    var activity = NSUserActivity(activityType: "panel")
    UIApplication.shared.requestSceneSessionActivation(nil, userActivity: activity, options: nil) { (error) in

    }
}

为您的新场景创建一个新的故事板,至少创建一个视图控制器,并确保将其设置为故事板中的initalviewcontroller。

让我们添加以下功能:

func application(_ application: UIApplication, configurationForConnecting connectingSceneSession: UISceneSession, options: UIScene.ConnectionOptions) -> UISceneConfiguration { 
        if options.userActivities.first?.activityType == "panel" {
            let configuration = UISceneConfiguration(name: "Default Configuration", sessionRole: connectingSceneSession.role)
            configuration.delegateClass = CustomSceneDelegate.self
            configuration.storyboard = UIStoryboard(name: "CustomScene", bundle: Bundle.main)
            return configuration
        } else {
            let configuration = UISceneConfiguration(name: "Default Configuration", sessionRole: connectingSceneSession.role)
            configuration.delegateClass = SceneDelegate.self
            configuration.storyboard = UIStoryboard(name: "Main", bundle: Bundle.main)
            return configuration
        }
    }

通过在请求场景时设置userActivity,我们可以知道要创建哪个场景并相应地为其创建配置。菜单或CMD + N中的“新窗口”仍将创建您的默认新窗口,但是“新窗口”按钮现在将通过新的故事板创建UI。

和多田:

enter image description here

© www.soinside.com 2019 - 2024. All rights reserved.