如何使用 SwiftUI 将项目添加到 NSToolbar?

Jaa*_*nus 8 macos nstoolbar swiftui

考虑使用应用程序委托生命周期实现的 macOS 应用程序代码:

应用程序委托:

func applicationDidFinishLaunching(_ aNotification: Notification) {
        let contentView = ContentView()
        window = NSWindow(
            contentRect: NSRect(x: 0, y: 0, width: 480, height: 300),
            styleMask: [.titled, .closable, .miniaturizable, .resizable, .fullSizeContentView],
            backing: .buffered, defer: false)
        window.toolbar = NSToolbar(identifier: "toolbar")
        window.title = "hello title"
        window.subtitle = "hello subtitle"
        window.isReleasedWhenClosed = false
        window.center()
        window.setFrameAutosaveName("Main Window")
        window.contentView = NSHostingView(rootView: contentView)
        window.makeKeyAndOrderFront(nil)
    }
Run Code Online (Sandbox Code Playgroud)

斯威夫特用户界面:

struct ContentView: View {
    var body: some View {
        NavigationView {
            List {
                NavigationLink(
                    destination: ItemView(),
                    label: {
                        Label("Something", systemImage: "folder.circle")
                    })
                NavigationLink(
                    destination: ItemView(),
                    label: {
                        Label("Another", systemImage: "pencil.tip.crop.circle")
                    })
            }.listStyle(SidebarListStyle())
        }
        .frame(maxWidth: .infinity, maxHeight: .infinity)
        .toolbar {
            ToolbarItem {
                Button(action: {
                    print("Button clicked")
                }, label: {
                    Label("Hello", systemImage: "pencil.circle")
                })
            }
        }
    }
}
struct ItemView: View {
    var body: some View {
        Text("Hello, World!")
            .frame(maxWidth: .infinity, maxHeight: .infinity)
            .toolbar {
                ToolbarItem {
                    Button(action: {
                        print("Item button")
                    }, label: {
                        Label("Itemtool", systemImage: "lock.doc")
                    })
                }
            }
    }
}
Run Code Online (Sandbox Code Playgroud)

其作用:正确设置窗口和工具栏外观。但工具栏没有按钮。它看起来像这样:

测试窗口

我想做的:将 SwiftUI 添加的工具栏项添加到工具栏。

当我让窗口由 SwiftUI 应用程序生命周期管理时,一切都会按预期工作。但是,由于不相关的原因,我需要坚持应用程序委托生命周期。

这可能吗?如何将 SwiftUI 中的工具栏按钮添加到应用程序委托创建的窗口的工具栏?

小智 -2

在 macOS 11 beta 6 和 Xcode 12 beta 6 上:

内容查看:

import SwiftUI

struct ContentView: View {
    var body: some View {
        NavigationView {
            List {
                NavigationLink(
                    destination: ItemView(),
                    label: {
                        Label("Something", systemImage: "folder.circle")
                    })
                NavigationLink(
                    destination: ItemView(),
                    label: {
                        Label("Another", systemImage: "pencil.tip.crop.circle")
                    })
            }.listStyle(SidebarListStyle())
            .toolbar {
                ToolbarItem {
                    Button(action: {
                        print("Button clicked")
                    }, label: {
                        Label("Hello", systemImage: "pencil.circle")
                    })
                }
            }
        }
        .frame(maxWidth: .infinity, maxHeight: .infinity)
        /* move into the NavigationView
        .toolbar {
            ToolbarItem {
                Button(action: {
                    print("Button clicked")
                }, label: {
                    Label("Hello", systemImage: "pencil.circle")
                })
            }
        }
        */
 
    }
}

struct ItemView: View {
    var body: some View {
        Text("Hello, World!")
            .frame(maxWidth: .infinity, maxHeight: .infinity)
            .navigationTitle("hello title")  // add title
            .navigationSubtitle("hello subtitle")  // add sub title
            .toolbar {
                ToolbarItem {
                    Button(action: {
                        print("Item button")
                    }, label: {
                        Label("Itemtool", systemImage: "lock.doc")
                    })
                }
            }
    }
}
Run Code Online (Sandbox Code Playgroud)

你需要一个 Appdelegate 类:

class AppDelegate: NSObject, NSApplicationDelegate, NSWindowDelegate {
    
    func applicationDidFinishLaunching(_ aNotification: Notification) {
    // do something
    }

    // others func

}
Run Code Online (Sandbox Code Playgroud)

最后的:

@main
struct SomeApp: App {
    
    @NSApplicationDelegateAdaptor(AppDelegate.self) var appDelegate

    var body: some Scene {
        WindowGroup {
            ContentView()
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

结果:

在此输入图像描述

  • 这展示了如何使用新的 SwiftUI 应用程序生命周期添加工具栏按钮。这对我来说效果很好。我的问题是如何使用 App Delegate 生命周期来做到这一点(没有 SomeApp 类,没有 App 子类)。 (3认同)