在SwiftUI macOS中的帮助菜单下添加应用程序帮助

4
如果您打开任何应用程序,它都有位于顶部的菜单栏项目,最后一个是帮助。 当我现在运行我的SwiftUI macOs应用程序并按下菜单上的帮助,然后点击“(Application Name) help”,它只显示“(Application Name)的帮助不可用”。如何添加对此的支持?

你是否正在使用SwiftUI的生命周期?你打算使用标准的帮助文档格式还是展示自己的帮助视图? - undefined
1
我正在使用SwiftUI的生命周期,并打算使用标准的帮助文档格式。然而,我也很想知道如何为了简化帮助而创建自己的视图。 - undefined
这个回答解决了你的问题吗?SwiftUI:如何在macOS应用程序中实现编辑菜单 - undefined
好的,这回答了关于制作帮助菜单自定义视图的两个问题中的一个。如果我想要调整标准文档格式,该怎么办呢? - undefined
1
实际上,我已经尝试过了,我发现我可以用那个来替换帮助按钮,但是当按钮被按下时,我该如何显示一个视图呢? - undefined
1个回答

0
我以为我在一段时间前回答了我的问题,因为我找到了一个解决办法,但看起来我并没有。对于那些在这里寻找答案的人,这是一个可能的解决途径。

MyApp.swift

struct MyApp: App {
    @State private var helpOpened = false
    
    var body: some Scene {
        WindowGroup {
            ContentView()
        }
        .commands {
            CommandGroup(replacing: .help) {
                Button(action: {
                    if !helpOpened {
                        helpOpened.toggle()
                        HelpView(isOpen: $helpOpened).openNewWindow(with: "Help")
                    }
                }, label: {
                    Text("MyApp Help")
                })
                .keyboardShortcut("/")
            }
        }
    }
}

extension View {
    private func newWindowInternal(with title: String) -> NSWindow {
        let window = NSWindow(
            contentRect: NSRect(x: 20, y: 20, width: 320, height: 420),
            styleMask: [.titled, .miniaturizable, .resizable, .closable, .fullSizeContentView],
            backing: .buffered,
            defer: false)
        window.center()
        window.isReleasedWhenClosed = false
        window.title = title
        window.makeKeyAndOrderFront(nil)
        return window
    }
    
    func openNewWindow(with title: String = "new Window") {
        self.newWindowInternal(with: title).contentView = NSHostingView(rootView: self)
    }
}

HelpView.swift

struct HelpView: View {
    @Binding var isOpen: Bool

    var body: some View {
        VStack {
            Text("Help View")
                .padding()
        }
        .onDisappear {
            isOpen = false
        }
        .onAppear {
            isOpen = true
        }
    }
}

ContentView.swift

struct ContentView: View {
    var body: some View {
        VStack {
            Text("Hello World!")
                .padding()
        }
    }
}

结果

当您运行应用程序时,您会发现按下“帮助”和“我的应用程序帮助”按钮将启动自定义视图。

您也可以使用以下键盘命令⌘/⌘?)来打开帮助。

希望这对您有所帮助 :)


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接