Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I disable the close button?

Tags:

macos

swift

cocoa

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!

like image 739
TDM Avatar asked Jan 19 '16 20:01

TDM


2 Answers

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)
like image 169
Darren Avatar answered Oct 16 '22 13:10

Darren


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

like image 21
Peter Ahlberg Avatar answered Oct 16 '22 13:10

Peter Ahlberg