如果您打开任何应用程序,它都有位于顶部的菜单栏项目,最后一个是帮助。 当我现在运行我的SwiftUI macOs应用程序并按下菜单上的帮助,然后点击“(Application Name) help”,它只显示“(Application Name)的帮助不可用”。如何添加对此的支持?
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)
}
}
struct HelpView: View {
@Binding var isOpen: Bool
var body: some View {
VStack {
Text("Help View")
.padding()
}
.onDisappear {
isOpen = false
}
.onAppear {
isOpen = true
}
}
}
struct ContentView: View {
var body: some View {
VStack {
Text("Hello World!")
.padding()
}
}
}
当您运行应用程序时,您会发现按下“帮助”和“我的应用程序帮助”按钮将启动自定义视图。
您也可以使用以下键盘命令⌘/(⌘?)来打开帮助。
希望这对您有所帮助 :)