您可以做到这一点,但它确实需要创建一个 AppDelegate。您的 AppFile 应该如下所示:
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")
}
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
你的 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)
}
}
Run Code Online (Sandbox Code Playgroud)
然后,只需创建一个名为 AboutView 的 SwiftUI 视图,它就会在您的“关于”框中显示该视图。例如:
struct AboutView: View {
var body: some View {
VStack {
Spacer()
HStack {
Spacer()
Text("Hello, World!")
Spacer()
}
Spacer()
}
.frame(minWidth: 300, minHeight: 300)
}
}
Run Code Online (Sandbox Code Playgroud)
import Foundation\nimport SwiftUI\n\nstruct AboutView: View {\n var body: some View {\n VStack(spacing: 10) {\n Image(nsImage: NSImage(named: "AppIcon")!)\n \n Text("\\(Bundle.main.appName)")\n .font(.system(size: 20, weight: .bold))\n // Xcode 13.0 beta 2\n //.textSelection(.enabled)\n \n Link("\\(AboutView.offSiteAdr.replace(of: "http://", to: ""))", destination: AboutView.offCiteUrl )\n \n Text("Ver: \\(Bundle.main.appVersionLong) (\\(Bundle.main.appBuild)) ")\n // Xcode 13.0 beta 2\n //.textSelection(.enabled)\n \n Text(Bundle.main.copyright)\n .font(.system(size: 10, weight: .thin))\n .multilineTextAlignment(.center)\n }\n .padding(20)\n .frame(minWidth: 350, minHeight: 300)\n }\n}\n\n///////////////////////////////////\n/// HELPERS\n//////////////////////////////////\nclass AppDelegate: NSObject, NSApplicationDelegate {\n private var aboutBoxWindowController: NSWindowController?\n \n func showAboutWnd() {\n if aboutBoxWindowController == nil {\n let styleMask: NSWindow.StyleMask = [.closable, .miniaturizable,/* .resizable,*/ .titled]\n let window = NSWindow()\n window.styleMask = styleMask\n window.title = "About \\(Bundle.main.appName)"\n window.contentView = NSHostingView(rootView: AboutView())\n window.center()\n aboutBoxWindowController = NSWindowController(window: window)\n }\n \n aboutBoxWindowController?.showWindow(aboutBoxWindowController?.window)\n }\n}\n\nextension AboutView {\n private static var offSiteAdr: String { "http://www.taogit.com" }\n private static var offEmail: String { "someUser@gmail.com" }\n \n public static var offCiteUrl: URL { URL(string: AboutView.offSiteAdr )! }\n public static var offEmailUrl: URL { URL(string: "mailto:\\(AboutView.offEmail)")! }\n}\nextension Bundle {\n public var appName: String { getInfo("CFBundleName") }\n //public var displayName: String {getInfo("CFBundleDisplayName")}\n //public var language: String {getInfo("CFBundleDevelopmentRegion")}\n //public var identifier: String {getInfo("CFBundleIdentifier")}\n public var copyright: String {getInfo("NSHumanReadableCopyright").replace(of: "\\\\\\\\n", to: "\\n") }\n \n public var appBuild: String { getInfo("CFBundleVersion") }\n public var appVersionLong: String { getInfo("CFBundleShortVersionString") }\n //public var appVersionShort: String { getInfo("CFBundleShortVersion") }\n \n fileprivate func getInfo(_ str: String) -> String { infoDictionary?[str] as? String ?? "\xe2\x9a\xa0\xef\xb8\x8f" }\n}\n\nRun Code Online (Sandbox Code Playgroud)\n并分配给菜单行:
\nstruct MyApp: App {\n @NSApplicationDelegateAdaptor(AppDelegate.self) var appDelegate\n\n var body: some Scene {\n WindowGroup {\n MainView()\n }\n // Replacement of standard About window\n .commands {\n CommandGroup(replacing: CommandGroupPlacement.appInfo) {\n Button("About \\(Bundle.main.appName)") { appDelegate.showAboutWnd() }\n }\n }\n }\n}\nRun Code Online (Sandbox Code Playgroud)\n结果:
\n\n奖励:支持版权中的“\\n”
\n\n| 归档时间: |
|
| 查看次数: |
2498 次 |
| 最近记录: |