1

Im trying to develop an OS X cocoa application programmatically, and I'm trying to display a window with a title bar which displays the usual traffic light (close, minimise, fullscreen) options at the top. However, when the window displays on the screen, there is just an empty window.

Here is the code I am using:

class AppDelegate: NSObject, NSApplicationDelegate {

let window = NSWindow(contentRect: NSMakeRect(10, 10, 200, 200),
                        styleMask: NSWindowStyleMask.closable,
                        backing: NSBackingStoreType.buffered, defer: true)

func applicationDidFinishLaunching(_ aNotification: Notification) {

    self.titleVisibility = .visible;
    self.titlebarAppearsTransparent = false;
    self.isMovableByWindowBackground  = true;

    let controller = NSWindowController(window: window)
    controller.showWindow(self);

}

I have tried different NSWindowStyleMask when constructing the NSWindow but to no success.

This is what I see:

black window with no titlebar

I am using Xcode 8.3 on 10.12

Tyler Durden
  • 575
  • 7
  • 23
  • http://stackoverflow.com/a/29339315/2303865 – Leo Dabus Apr 08 '17 at 11:44
  • The problem is that you need to add .titled (`[.titled, .closable]`) to your styleMask otherwise the title bar and all buttons (close, resize and minimize) will be hidden. – Leo Dabus Apr 08 '17 at 11:50

1 Answers1

2

So you need 4 style masks.

NSWindowStyleMask.closable NSWindowStyleMask.miniaturizable NSWindowStyleMask.resizable NSWindowStyleMask.titled

To put them all into one, you can use array

[NSWindowStyleMask.closable, NSWindowStyleMask.miniaturizable, NSWindowStyleMask.resizable, NSWindowStyleMask.titled]

But try to write in swift style

[.closable, .miniaturizable, .resizable, .titled]
  • He doesn't need all of them. He only needs to add titled to the styleMask optionSet. – Leo Dabus Apr 08 '17 at 11:55
  • I initially tried the `.titled` style mask, but i get the exception `2017-04-08 12:57:25.982647+0100 X[8590:1030549] *** Assertion failure in -[X.XApplication init], /Library/Caches/com.apple.xbs/Sources/AppKit/AppKit-1504.82.104/AppKit.subproj/NSApplication.m:1778` and the same with suggested array too. – Tyler Durden Apr 08 '17 at 11:59
  • Check the link I posted above – Leo Dabus Apr 08 '17 at 12:42
  • If you just add `window.styleMask.insert(.titled)` in your applicationDidFinishLaunching method, it should display the title bar and the close button. – Leo Dabus Apr 08 '17 at 14:17
  • @LeoDabus adding .titled to the init made it crash but adding it to the through the insert method worked. Im not sure why. Could you explain? Also would you like to post this in an answer and ill accept it as the correct one. – Tyler Durden Apr 08 '17 at 16:26
  • @TylerDurden I don't know what you mean but try `let window = NSWindow(contentRect: NSMakeRect(10, 10, 200, 200), styleMask: [.closable, .titled], backing: .buffered, defer: true)` should work also – Leo Dabus Apr 09 '17 at 00:26