SwiftUImacosNSWindow实例
使用xcode 12.3和swift 5.3与SwiftUI App生命周期构建一个macOS应用程序,什么是最好的方式来访问和更改的外观和行为NSWindow?
编辑:我真正追求的是NSWindow实例。
我添加了一个AppDelegate,但据我了解它NSWindow很可能是nil,因此无法修改,只需在此处创建一个类似于AppKit App Delegate生命周期方法的方法,就会在启动时出现两个窗口。
一种解决方案是防止默认窗口出现,并将其全部留给applicationDidFinishLaunching方法,但不确定这是可能的或明智的。
该WindowStyle协议看起来是一种可能的解决方案,但不确定CustomWindowStyle在此阶段如何最好地利用它,以及是否提供对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()
}
}
}
回答
虽然我不完全确定这是否是正确的方法,但基于对这个问题的回答:https : //stackoverflow.com/a/63439982/792406我已经能够访问该NSWindow实例并修改其外观。
为了便于参考,这里的基础上所提供的原始代码的工作示例Asperi使用xcode 12.3,swift 5.3以及SwiftUI应用程序生命周期。
@main
struct testApp: App {
var body: some Scene {
WindowGroup {
ContentView()
}
}
}
class Store {
var window: NSWindow
init(window: NSWindow) {
self.window = window
self.window.isOpaque = false
self.window.backgroundColor = NSColor.clear
}
}
struct ContentView: View {
@State private var window: NSWindow?
var body: some View {
VStack {
Text("Loading...")
if nil != window {
MainView(store: Store(window: window!))
}
}.background(WindowAccessor(window: $window))
}
}
struct MainView: View {
let store: Store
var body: some View {
VStack {
Text("MainView with Window: (store.window)")
}.frame(width: 400, height: 400)
}
}
struct WindowAccessor: NSViewRepresentable {
@Binding var window: NSWindow?
func makeNSView(context: Context) -> NSView {
let view = NSView()
DispatchQueue.main.async {
self.window = view.window
}
return view
}
func updateNSView(_ nsView: NSView, context: Context) {}
}