SwiftUI:更改 macOS 应用程序中的“关于视图”
SwiftUI: Change “About View” in macOS app
我正在使用 SwiftUI
和新的 App
生命周期构建一个 macOS 应用程序。
我很想更改“关于 Window”(当您在应用程序菜单中点击“关于 DemoApp”时出现)的内容,但不知道如何更改:
如何用自定义视图替换“关于”视图?
您需要将 Credits.rtf 文件添加到您的 Bundle。这将被自动检测并插入到“关于”对话框中。
您可以找到更多 here
您可以做到,但它需要创建一个 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")
}
}
}
}
}
您的 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 的 SwiftUI 视图,它就会显示在您的关于框中。例如:
struct AboutView: View {
var body: some View {
VStack {
Spacer()
HStack {
Spacer()
Text("Hello, World!")
Spacer()
}
Spacer()
}
.frame(minWidth: 300, minHeight: 300)
}
}
import Foundation
import SwiftUI
struct AboutView: View {
var body: some View {
VStack(spacing: 10) {
Image(nsImage: NSImage(named: "AppIcon")!)
Text("\(Bundle.main.appName)")
.font(.system(size: 20, weight: .bold))
// Xcode 13.0 beta 2
//.textSelection(.enabled)
Link("\(AboutView.offSiteAdr.replace(of: "http://", to: ""))", destination: AboutView.offCiteUrl )
Text("Ver: \(Bundle.main.appVersionLong) (\(Bundle.main.appBuild)) ")
// Xcode 13.0 beta 2
//.textSelection(.enabled)
Text(Bundle.main.copyright)
.font(.system(size: 10, weight: .thin))
.multilineTextAlignment(.center)
}
.padding(20)
.frame(minWidth: 350, minHeight: 300)
}
}
///////////////////////////////////
/// HELPERS
//////////////////////////////////
class AppDelegate: NSObject, NSApplicationDelegate {
private var aboutBoxWindowController: NSWindowController?
func showAboutWnd() {
if aboutBoxWindowController == nil {
let styleMask: NSWindow.StyleMask = [.closable, .miniaturizable,/* .resizable,*/ .titled]
let window = NSWindow()
window.styleMask = styleMask
window.title = "About \(Bundle.main.appName)"
window.contentView = NSHostingView(rootView: AboutView())
window.center()
aboutBoxWindowController = NSWindowController(window: window)
}
aboutBoxWindowController?.showWindow(aboutBoxWindowController?.window)
}
}
extension AboutView {
private static var offSiteAdr: String { "http://www.taogit.com" }
private static var offEmail: String { "someUser@gmail.com" }
public static var offCiteUrl: URL { URL(string: AboutView.offSiteAdr )! }
public static var offEmailUrl: URL { URL(string: "mailto:\(AboutView.offEmail)")! }
}
extension Bundle {
public var appName: String { getInfo("CFBundleName") }
//public var displayName: String {getInfo("CFBundleDisplayName")}
//public var language: String {getInfo("CFBundleDevelopmentRegion")}
//public var identifier: String {getInfo("CFBundleIdentifier")}
public var copyright: String {getInfo("NSHumanReadableCopyright").replace(of: "\\n", to: "\n") }
public var appBuild: String { getInfo("CFBundleVersion") }
public var appVersionLong: String { getInfo("CFBundleShortVersionString") }
//public var appVersionShort: String { getInfo("CFBundleShortVersion") }
fileprivate func getInfo(_ str: String) -> String { infoDictionary?[str] as? String ?? "⚠️" }
}
并分配给菜单行:
struct MyApp: App {
@NSApplicationDelegateAdaptor(AppDelegate.self) var appDelegate
var body: some Scene {
WindowGroup {
MainView()
}
// Replacement of standard About window
.commands {
CommandGroup(replacing: CommandGroupPlacement.appInfo) {
Button("About \(Bundle.main.appName)") { appDelegate.showAboutWnd() }
}
}
}
}
结果:
奖励:支持版权中的“\n”
我正在使用 SwiftUI
和新的 App
生命周期构建一个 macOS 应用程序。
我很想更改“关于 Window”(当您在应用程序菜单中点击“关于 DemoApp”时出现)的内容,但不知道如何更改:
如何用自定义视图替换“关于”视图?
您需要将 Credits.rtf 文件添加到您的 Bundle。这将被自动检测并插入到“关于”对话框中。
您可以找到更多 here
您可以做到,但它需要创建一个 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")
}
}
}
}
}
您的 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 的 SwiftUI 视图,它就会显示在您的关于框中。例如:
struct AboutView: View {
var body: some View {
VStack {
Spacer()
HStack {
Spacer()
Text("Hello, World!")
Spacer()
}
Spacer()
}
.frame(minWidth: 300, minHeight: 300)
}
}
import Foundation
import SwiftUI
struct AboutView: View {
var body: some View {
VStack(spacing: 10) {
Image(nsImage: NSImage(named: "AppIcon")!)
Text("\(Bundle.main.appName)")
.font(.system(size: 20, weight: .bold))
// Xcode 13.0 beta 2
//.textSelection(.enabled)
Link("\(AboutView.offSiteAdr.replace(of: "http://", to: ""))", destination: AboutView.offCiteUrl )
Text("Ver: \(Bundle.main.appVersionLong) (\(Bundle.main.appBuild)) ")
// Xcode 13.0 beta 2
//.textSelection(.enabled)
Text(Bundle.main.copyright)
.font(.system(size: 10, weight: .thin))
.multilineTextAlignment(.center)
}
.padding(20)
.frame(minWidth: 350, minHeight: 300)
}
}
///////////////////////////////////
/// HELPERS
//////////////////////////////////
class AppDelegate: NSObject, NSApplicationDelegate {
private var aboutBoxWindowController: NSWindowController?
func showAboutWnd() {
if aboutBoxWindowController == nil {
let styleMask: NSWindow.StyleMask = [.closable, .miniaturizable,/* .resizable,*/ .titled]
let window = NSWindow()
window.styleMask = styleMask
window.title = "About \(Bundle.main.appName)"
window.contentView = NSHostingView(rootView: AboutView())
window.center()
aboutBoxWindowController = NSWindowController(window: window)
}
aboutBoxWindowController?.showWindow(aboutBoxWindowController?.window)
}
}
extension AboutView {
private static var offSiteAdr: String { "http://www.taogit.com" }
private static var offEmail: String { "someUser@gmail.com" }
public static var offCiteUrl: URL { URL(string: AboutView.offSiteAdr )! }
public static var offEmailUrl: URL { URL(string: "mailto:\(AboutView.offEmail)")! }
}
extension Bundle {
public var appName: String { getInfo("CFBundleName") }
//public var displayName: String {getInfo("CFBundleDisplayName")}
//public var language: String {getInfo("CFBundleDevelopmentRegion")}
//public var identifier: String {getInfo("CFBundleIdentifier")}
public var copyright: String {getInfo("NSHumanReadableCopyright").replace(of: "\\n", to: "\n") }
public var appBuild: String { getInfo("CFBundleVersion") }
public var appVersionLong: String { getInfo("CFBundleShortVersionString") }
//public var appVersionShort: String { getInfo("CFBundleShortVersion") }
fileprivate func getInfo(_ str: String) -> String { infoDictionary?[str] as? String ?? "⚠️" }
}
并分配给菜单行:
struct MyApp: App {
@NSApplicationDelegateAdaptor(AppDelegate.self) var appDelegate
var body: some Scene {
WindowGroup {
MainView()
}
// Replacement of standard About window
.commands {
CommandGroup(replacing: CommandGroupPlacement.appInfo) {
Button("About \(Bundle.main.appName)") { appDelegate.showAboutWnd() }
}
}
}
}
结果:
奖励:支持版权中的“\n”