How to disable Full Screen button with SwiftUI 2 on macOS

Viewed 965

As suggested with the title, how should I disable Full Screen button with SwiftUI 2 on macOS?

The only information I could find seems to use features from NSWindow. Is there a native way to do that in SwiftUI 2?

2 Answers

You can just simply use .onReceive modifier to achieve the purpose:

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())
}

The effect should be as following, the third green button becomes transparent gray: enter image description here

YourAppName.Swift

import SwiftUI

@main
struct YourApp: App {    
    var body: some Scene {
        WindowGroup {
            ContentView()
                .onAppear {
                    DispatchQueue.main.async {
                        NSApplication.shared.windows.forEach { window in
                            window.styleMask = [.titled, .closable, .miniaturizable]
                        }
                    }
                }
        }
    }
}

Related