'How can I disable the close button?

I need some help with figuring out how to disable/hide the close, minimize, and resize buttons in OS X Cocoa and Swift 2. Here's the code I tried. I know it's for the Title Bar, but I thought I'd try it anyway:

self.window.titleVisibility = NSWindowTitleVisibility.Hidden;

Does any one know how to do that? I'm using Swift 2, OS X Cocoa, and Xcode 7.2. Thanks!



Solution 1:[1]

Also try;

    self.window!.standardWindowButton(NSWindow.ButtonType.closeButton)!.hidden = true
    self.window!.standardWindowButton(NSWindow.ButtonType.miniaturizeButton)!.hidden = true

etc.

Solution 2:[2]

In Xcode 9.1 you can use following in the ViewController,

override func viewWillAppear() {

    self.view.window?.titleVisibility = .hidden
    self.view.window?.titlebarAppearsTransparent = true

    self.view.window?.styleMask.insert(.fullSizeContentView)

    self.view.window?.styleMask.remove(.closable)
    self.view.window?.styleMask.remove(.fullScreen)
    self.view.window?.styleMask.remove(.miniaturizable)
    self.view.window?.styleMask.remove(.resizable)

    //self.view.window?.isMovable = false
}

enter image description here

override func viewWillAppear() {

    self.view.window?.titleVisibility = .hidden
    self.view.window?.titlebarAppearsTransparent = true

    self.view.window?.styleMask.insert(.fullSizeContentView)

    //self.view.window?.styleMask.remove(.closable)
    self.view.window?.styleMask.remove(.fullScreen)
    self.view.window?.styleMask.remove(.miniaturizable)
    self.view.window?.styleMask.remove(.resizable)

    //self.view.window?.isMovable = false
}

enter image description here

Solution 3:[3]

See the NSWindow.styleMask property and the Window Style Masks.

Clearing the NSClosableWindowMask, NSMiniaturizableWindowMask, and NSResizableWindowMask flags will remove all of the buttons from a window's title bar.

window.styleMask &= ~(NSClosableWindowMask | NSMiniaturizableWindowMask | NSResizableWindowMask)

Solution 4:[4]

I find that the following effectively disables without completely hiding the close button:

self.view.window?.standardWindowButton(NSWindow.ButtonType.closeButton)?.isEnabled = false

Solution 5:[5]

a side note: do NOT disable "close" red button: apple will reject app saying:

"The user interface of your app is not consistent with the macOS Human Interface Guidelines. Specifically:

Specifically, the red light was disabled."

:(

Solution 6:[6]

If you are using Storyboards and you can change your window styleMask property inside an IBAction or viewDidLoad as follow:

NSApplication.sharedApplication().windows.first?.styleMask = NSTitledWindowMask   // | NSClosableWindowMask |  NSMiniaturizableWindowMask  | NSResizableWindowMask

If you would like to enable them again just uncomment the rest of the style mask:

NSApplication.sharedApplication().windows.first?.styleMask = NSTitledWindowMask | NSClosableWindowMask |  NSMiniaturizableWindowMask  | NSResizableWindowMask

Solution 7:[7]

Expanding on the accepted answer above by @JohnElemans, this is what worked for me when presenting modally using a storyboard segue on macOS 10.15:

// On NSViewController.viewDidAppear()

if let window = self.view.window {
    window.standardWindowButton(NSWindow.ButtonType.closeButton)?.isHidden = true
    window.standardWindowButton(NSWindow.ButtonType.miniaturizeButton)?.isHidden = true
    window.standardWindowButton(NSWindow.ButtonType.zoomButton)?.isHidden = true
}

I had to add the third line (NSWindow.ButtonType.zoomButton) to get rid of the green ('fullscreen') button.

Solution 8:[8]

If you are using Flutter? you can do like this?

import Cocoa
import FlutterMacOS
import window_manager


class MainFlutterWindow: NSWindow {
  override func awakeFromNib() {
    let flutterViewController = FlutterViewController.init()
    let windowFrame = self.frame
    self.contentViewController = flutterViewController
    self.setFrame(windowFrame, display: true)
  
  self.standardWindowButton(.closeButton)?.isHidden = true
  self.standardWindowButton(.miniaturizeButton)?.isHidden = true
  self.standardWindowButton(.zoomButton)?.isHidden = true


RegisterGeneratedPlugins(registry: flutterViewController)

super.awakeFromNib()
  }

    
  override public func order(_ place: NSWindow.OrderingMode, relativeTo otherWin: Int) {
            super.order(place, relativeTo: otherWin)
             hiddenWindowAtLaunch()
  }
}

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 cheesey
Solution 2
Solution 3 Darren
Solution 4 The Kraken
Solution 5 ingconti
Solution 6 Leo Dabus
Solution 7 Nicolas Miari
Solution 8 capdev