How to disable the Full Screen button with SwiftUI

In macOS, the full screen button is that green circle button in the menu bar, able to maximise a window to full screen.

But what if we are making an app where this feature would be pointless, or even ugly?
There is already an app made by Apple that has this feature disabled: I’m talking about System Preferences.

In System Preferences, the Full Screen button is disabled.

Usually, if we’re using the UI editor in Xcode, achieving this behaviour is as simple as unticking a checkbox. But what if we’re using SwiftUI?

Well, it turns out we have to edit the AppDelegate.swift file, more specifically we have to modify the styleMask constants.

The styleMask constants specify the style of a window:
i.e. the .miniaturizable constant will allow our window to be miniaturised, the .borderless constant will make the window to displays “none of the usual peripheral elements”, and so on…

Disabling the Full Screen capabilities of the window then, is as simple as removing the .resizable constant from the list of constants passed to the styleMask parameter, just like this:

// [...]
// Create the window and set the content view. 
window = NSWindow(
    contentRect: NSRect(x: 0, y: 0, width: 480, height: 300),
    styleMask: [.titled, .closable, .miniaturizable /* , .resizable */],
    backing: .buffered, defer: false)
// [...]

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.