SwiftUI macos NSWindow 实例

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

使用 Xcode 12.3 和 Swift 5.3 以及 SwiftUI 应用程序生命周期构建 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()
        }
    }
}
swift macos swiftui nswindow
2个回答
12
投票

虽然我不完全确定这是否是正确的方法,但基于这个问题的答案: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) {}
}

12
投票

有趣的方法@hillmark。

我还通过使用

NSApplicationDelegateAdaptor
的方法让它工作。

我相信下面的代码只能帮助您使用单窗口 MacOS SwiftUI 应用程序 - 多窗口应用程序可能需要处理所有窗口,而不是只处理第一个窗口。

我还不确定我的方法是否有任何警告。

目前这准确地解决了我的问题,但我也会检查你的方法。

import SwiftUI

final class AppDelegate: NSObject, NSApplicationDelegate {
    func applicationDidFinishLaunching(_ notification: Notification) {
        if let window = NSApplication.shared.windows.first {
            window.titleVisibility = .hidden
            window.titlebarAppearsTransparent = true
            window.isOpaque = false
            window.backgroundColor = NSColor.clear
        }
    }
}

@main
struct AlreadyMacOSApp: App {
    @NSApplicationDelegateAdaptor(AppDelegate.self) var delegate
    var body: some Scene {
        WindowGroup {
            NewLayoutTest()
        }
    }
}
© www.soinside.com 2019 - 2024. All rights reserved.