Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Create a new window with NSWindow

Tags:

swift

I'd like to create a new window programmatically. I have the following code, and it builds, but my window doesn't show up. The only way I can make it visible is by adding it as a child window to the default 'window'. How can I make 'win' be an independent window?

@IBOutlet var window: NSWindow

func applicationDidFinishLaunching(aNotification: NSNotification?) {

    var win = NSWindow(contentRect: NSMakeRect(100, 100, 600, 200),
                       styleMask: NSResizableWindowMask,
                       backing: NSBackingStoreType.Buffered, defer: true)

    window.addChildWindow(win, ordered:NSWindowOrderingMode.Above)
}
like image 445
Geuis Avatar asked Jun 05 '14 19:06

Geuis


2 Answers

What about adding:

win.makeKeyAndOrderFront(win)

For me on OSX (not iOS) using Swift and writing in vim

let win = NSWindow(contentRect: NSMakeRect(100, 100, 600, 200),
                           styleMask: NSResizableWindowMask,
                           backing: NSBackingStoreType.buffered, defer: true)

win.makeKeyAndOrderFront(win)

pops up a window

like image 183
DMS Avatar answered Oct 02 '22 10:10

DMS


You also need a NSWindowController to display the window:

let window = NSWindow(...)
let controller = NSWindowController(window: window)

controller.showWindow(self)
like image 26
Erik Avatar answered Oct 02 '22 11:10

Erik