当用户关闭最后一个窗口时,是否可以关闭 macOS SwiftUI 应用程序,类似于applicationShouldTerminateAfterLastWindowClosed
应用程序委托函数。
func applicationShouldTerminateAfterLastWindowClosed(NSApplication) -> Bool
我在这里找到了答案https://www.hackingwithswift.com/quick-start/swiftui/how-to-add-an-appdelegate-to-a-swiftui-app https://www.hackingwithswift.com/quick-start/swiftui/how-to-add-an-appdelegate-to-a-swiftui-app
为 AppDelegate 创建一个类
import Foundation
import AppKit
class AppDelegate: NSObject, NSApplicationDelegate {
func applicationShouldTerminateAfterLastWindowClosed(_ sender: NSApplication) -> Bool {
return true
}
}
将属性包装器添加到您的 SwiftUI App 类
import SwiftUI
@main
struct SwiftUIApp: App {
@NSApplicationDelegateAdaptor(AppDelegate.self) var appDelegate
var body: some Scene {
WindowGroup {
ContentView()
.frame(minWidth: 300, idealWidth: 300, maxWidth: .infinity, minHeight: 300, idealHeight: 300, maxHeight: .infinity)
}
}
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)