Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/17.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
SwiftUI:更改macOS应用程序中的“关于视图”_Swift_Swiftui_Menuitem_Nsmenu_About Box - Fatal编程技术网

SwiftUI:更改macOS应用程序中的“关于视图”

SwiftUI:更改macOS应用程序中的“关于视图”,swift,swiftui,menuitem,nsmenu,about-box,Swift,Swiftui,Menuitem,Nsmenu,About Box,我正在使用SwiftUI和新的应用程序生命周期构建一个macOS应用程序 我想更改当您点击应用程序菜单中的“关于DemoApp”时出现的“关于窗口”的内容,但不知道如何: 如何将“关于”视图替换为自定义视图?您需要将Credits.rtf文件添加到捆绑包中。这将自动检测并插入“关于”对话框 您可以找到更多信息您需要将Credits.rtf文件添加到捆绑包中。这将自动检测并插入“关于”对话框 您可以找到更多信息您可以这样做,但它确实需要创建一个AppDelegate。以下是应用程序文件的外观: s

我正在使用SwiftUI和新的应用程序生命周期构建一个macOS应用程序

我想更改当您点击应用程序菜单中的“关于DemoApp”时出现的“关于窗口”的内容,但不知道如何:


如何将“关于”视图替换为自定义视图?

您需要将Credits.rtf文件添加到捆绑包中。这将自动检测并插入“关于”对话框


您可以找到更多信息

您需要将Credits.rtf文件添加到捆绑包中。这将自动检测并插入“关于”对话框


您可以找到更多信息

您可以这样做,但它确实需要创建一个AppDelegate。以下是应用程序文件的外观:

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

    var body: some Scene {
        WindowGroup {
            MainView()
        }
        .commands {
            CommandGroup(replacing: CommandGroupPlacement.appInfo) {
                Button(action: {
                    appDelegate.showAboutPanel()
                }) {
                    Text("About My App")
                }
            }
        }
    }
}
您的AppDelegate应该如下所示:

class AppDelegate: NSObject, NSApplicationDelegate {
    private var aboutBoxWindowController: NSWindowController?

    func showAboutPanel() {
        if aboutBoxWindowController == nil {
            let styleMask: NSWindow.StyleMask = [.closable, .miniaturizable,/* .resizable,*/ .titled]
            let window = NSWindow()
            window.styleMask = styleMask
            window.title = "About My App"
            window.contentView = NSHostingView(rootView: AboutView())
            aboutBoxWindowController = NSWindowController(window: window)
        }

        aboutBoxWindowController?.showWindow(aboutBoxWindowController?.window)
    }
}
然后,只需创建一个名为AboutView的快速UI视图,它就会显示在您的About框中。例如:

struct AboutView: View {
    var body: some View {
        VStack {
            Spacer()
            HStack {
                Spacer()
                Text("Hello, World!")
                Spacer()
            }
            Spacer()
        }
        .frame(minWidth: 300, minHeight: 300)
    }
}

您可以这样做,但它确实需要创建AppDelegate。以下是应用程序文件的外观:

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

    var body: some Scene {
        WindowGroup {
            MainView()
        }
        .commands {
            CommandGroup(replacing: CommandGroupPlacement.appInfo) {
                Button(action: {
                    appDelegate.showAboutPanel()
                }) {
                    Text("About My App")
                }
            }
        }
    }
}
您的AppDelegate应该如下所示:

class AppDelegate: NSObject, NSApplicationDelegate {
    private var aboutBoxWindowController: NSWindowController?

    func showAboutPanel() {
        if aboutBoxWindowController == nil {
            let styleMask: NSWindow.StyleMask = [.closable, .miniaturizable,/* .resizable,*/ .titled]
            let window = NSWindow()
            window.styleMask = styleMask
            window.title = "About My App"
            window.contentView = NSHostingView(rootView: AboutView())
            aboutBoxWindowController = NSWindowController(window: window)
        }

        aboutBoxWindowController?.showWindow(aboutBoxWindowController?.window)
    }
}
然后,只需创建一个名为AboutView的快速UI视图,它就会显示在您的About框中。例如:

struct AboutView: View {
    var body: some View {
        VStack {
            Spacer()
            HStack {
                Spacer()
                Text("Hello, World!")
                Spacer()
            }
            Spacer()
        }
        .frame(minWidth: 300, minHeight: 300)
    }
}

非常感谢你的帮助,@davidev!如果您想向现有的“关于”面板添加积分,这将非常有用。不过,我想问的是如何用我自己的观点完全替换该面板,我不想只定制默认面板。非常感谢您的帮助,@davidev!如果您想向现有的“关于”面板添加积分,这将非常有用。然而,我问的是如何用我自己的观点完全替换这个面板,我不想只定制默认面板。