6

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?

4 Answers 4

10

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

Sign up to request clarification or add additional context in comments.

Comments

4

You can use .onAppear modifier to disable full screen button.

struct MacApp: App {
var body: some Scene {
   WindowGroup {
       ContentView()
           .onAppear {
               DispatchQueue.main.async {
                   NSApplication.shared.windows.forEach { window in
                       window.standardWindowButton(.zoomButton)?.isEnabled = false
                   }
               }
           }
   }
   .windowStyle(HiddenTitleBarWindowStyle())
}

1 Comment

This does not provide an answer to the question. Once you have sufficient reputation you will be able to comment on any post; instead, provide answers that don't require clarification from the asker. - From Review
2

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]
                        }
                    }
                }
        }
    }
}

1 Comment

1

As of macOS 13.0, you can define size for ContentView & set windowResizability to contentSize.

var body: some Scene {
    WindowGroup {
        ContentView().frame(width: 300, height: 400)
    }.windowResizability(.contentSize)
}

1 Comment

Thank you for the answer. One thing to note is while using a concrete size(as in your example) works perfectly; as of macOS 13.4 changing that to using a max size modifiers would leave a visible/clickable dummy fullscreen button, despite it won't function.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.