如果使用 xcode 12.3
和 swift 5.3
,以及 SwiftUI App
生命周期来构建一个 macOS
应用程序,那么访问和更改 NSWindow
的外观和行为的最佳方法是什么?
编辑: 我真正需要的是 NSWindow
实例。
我已经添加了一个 AppDelegate
,但据我所知,NSWindow
可能会是 nil
,因此无法修改,并且简单地在这里创建一个类似于 AppKit App Delegate
生命周期方法的窗口会导致启动时出现两个窗口。
其中一种解决方案是防止默认窗口出现,并将其全部交给 applicationDidFinishLaunching
方法,但不确定这是否可行或明智。
WindowStyle
协议似乎是一个可能的解决方案,但不确定如何在当前阶段以最佳方式利用它,以及它是否提供对 NSWindow
实例进行细粒度控制的访问权限。
class AppDelegate: NSObject, NSApplicationDelegate {
func applicationDidFinishLaunching(_ aNotification: Notification) {
// In AppKit simply create the NSWindow and modify style.
// In SwiftUI creating an NSWindow and styling results in 2 windows,
// one styled and the other default.
}
}
@main
struct testApp: App {
@NSApplicationDelegateAdaptor(AppDelegate.self) var appDelegate : AppDelegate
var body: some Scene {
WindowGroup {
ContentView()
}
}
}