首页 > 解决方案 > 如何在 macOS 上使用 SwiftUI 2 禁用全屏按钮

问题描述

如标题所示,我应该如何在 macOS 上使用 SwiftUI 2 禁用全屏按钮?

我能找到的唯一信息似乎是使用NSWindow. 在 SwiftUI 2 中是否有本地方法可以做到这一点?

标签: swiftuser-interfaceswiftui

解决方案


您可以简单地使用.onReceive修饰符来达到目的:

struct MacApp: App {
var body: some Scene {
    WindowGroup {
        ContentView()
            .frame(width: 480, height: 272)
            .fixedSize()
            .onReceive(NotificationCenter.default.publisher(for: NSApplication.willUpdateNotification), perform: { _ in
                for window in NSApplication.shared.windows {
                    window.standardWindowButton(.zoomButton)?.isEnabled = false
                }
            })
    }
    .windowStyle(HiddenTitleBarWindowStyle())
}

效果应该如下,第三个绿色按钮变成透明灰色: 在此处输入图像描述


推荐阅读