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?
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?
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:

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