【问题标题】:How do I add items to NSToolbar with SwiftUI?如何使用 SwiftUI 将项目添加到 NSToolbar?
【发布时间】:2020-12-18 00:38:18
【问题描述】:

考虑使用应用委托生命周期实现的这个 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)
    }

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())
        }
        .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")
                    })
                }
            }
    }
}

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

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

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

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

【问题讨论】:

    标签: macos swiftui nstoolbar


    【解决方案1】:

    在 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")
                        })
                    }
                }
        }
    }
    

    你需要一个 Appdelegate 类:

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

    最后:

    @main
    struct SomeApp: App {
        
        @NSApplicationDelegateAdaptor(AppDelegate.self) var appDelegate
    
        var body: some Scene {
            WindowGroup {
                ContentView()
            }
        }
    }
    

    结果:

    enter image description here

    【讨论】:

    • 这显示了如何使用新的 SwiftUI 应用程序生命周期添加工具栏按钮。这对我来说很好。我的问题是如何使用 App Delegate 生命周期来做到这一点(没有 SomeApp 类,没有 App 子类)。
    猜你喜欢
    • 2020-06-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-09-18
    • 1970-01-01
    相关资源
    最近更新 更多