gpt4 book ai didi

ios - 在 Mac Catalyst 中打开一个新窗口

转载 作者:行者123 更新时间:2023-12-01 19:36:25 27 4
gpt4 key购买 nike

我正在使用 Mac Catalyst 移植 iPad 应用程序。我正在尝试在新窗口中打开 View Controller 。

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

This article指出这可以通过在项目的新包中添加 AppKit 来实现(我就是这样做的),但是它没有解释如何实际呈现新窗口的具体细节。上面写着……

Another thing you cannot quite do is spawn a new NSWindow with a UIKit view hierarchy. However, your UIKit code has the ability to spawn a new window scene, and your AppKit code has the ability to take the resulting NSWindow it's presented in and hijack it to do whatever you want with it, so in that sense you could spawn UIKit windows for auxiliary palettes and all kinds of other features.

有人知道如何实现本文中解释的内容吗?

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

最佳答案

编辑:添加了有关如何拥有其他不同窗口(例如面板)的信息

为了在 Mac 上支持多窗口,您所需要做的就是按照在 iPad 上支持多窗口的方式操作。

您可以在this中找到您需要的所有信息WWDC session 从 22:28 开始,但总而言之,您需要做的是支持新的场景生命周期模型。

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

enter image description here

完成此操作后,单击配置选项,该选项将带您进入 info.plist。确保您有正确的应用程序场景 list 条目

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

}
}

为您的新场景创建一个新的 Storyboard,创建至少一个 View Controller ,并确保在 Storyboard中设置为 initalviewcontroller。

让我们向 appdelegate 添加以下函数:

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 仍将创建默认的新窗口,但“新窗口”按钮现在将从新 Storyboard创建 UI。

还有塔达:

enter image description here

关于ios - 在 Mac Catalyst 中打开一个新窗口,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58882047/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com