Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Programmatically create NSWindow

I want to programmatically create a new NSWindow, but I can't find a way to succeed. This simple code doesn't display a new window. What's wrong with it?

@NSApplicationMain
class AppDelegate: NSObject, NSApplicationDelegate {

    @IBOutlet weak var window: NSWindow!

    func applicationDidFinishLaunching(aNotification: NSNotification) {
        let win = NSWindow(contentRect: NSMakeRect(100, 100, 600, 200),
            styleMask: NSResizableWindowMask,
            backing: NSBackingStoreType.Buffered, defer: true)

        let controller = NSWindowController(window: win)

        controller.showWindow(self)
        win.makeKeyAndOrderFront(win)
    }
}
like image 279
user3345978 Avatar asked Jan 19 '15 19:01

user3345978


2 Answers

Xcode 11 • Swift 5.1

import Cocoa

@NSApplicationMain
class AppDelegate: NSObject, NSApplicationDelegate {
    @IBOutlet weak var window: NSWindow!
    let newWindow = NSWindow(contentRect: .init(origin: .zero,
                                                size: .init(width: NSScreen.main!.frame.midX,
                                                            height: NSScreen.main!.frame.midY)),
                             styleMask: [.closable],
                             backing: .buffered,
                             defer: false)
    func createNewWindow() {
        newWindow.title = "New Window"
        newWindow.isOpaque = false
        newWindow.center()
        newWindow.isMovableByWindowBackground = true
        newWindow.backgroundColor = NSColor(calibratedHue: 0, saturation: 1.0, brightness: 0, alpha: 0.7)
        newWindow.makeKeyAndOrderFront(nil)
    }
    func applicationDidFinishLaunching(_ aNotification: Notification) {
        createNewWindow()
    }
    func applicationWillTerminate(_ aNotification: Notification) {
        // Insert code here to tear down your application
    }
}

Window Style Mask

Sample project

like image 141
Leo Dabus Avatar answered Sep 17 '22 12:09

Leo Dabus


Here's a working example without any bells & whistles, updated to Swift 4.2.

It creates a bare window, with & without a nib depending on which function you call.

In IB, uncheck "initial controller" everywhere. For the nib version to work, you must give the IB-supplied window controller a storyboard ID of "WindowController."

    import Cocoa

    @NSApplicationMain
    class AppDelegate: NSObject, NSApplicationDelegate, DebugHelper {
        var myName: String = "AppDelegate"
        var windowController: NSWindowController!
        var window: NSWindow!

        func applicationDidFinishLaunching(_ aNotification: Notification) {
            //nib()
            diy()
        }
        func nib() {
            let storyboard = NSStoryboard(name: NSStoryboard.Name("Main"), bundle: nil)
            guard let wc = storyboard.instantiateController(withIdentifier: "WindowController") as? NSWindowController else { return }
            windowController = wc
            wc.showWindow(self)
        }
        func diy() {
            window = NSWindow()
            window.styleMask = NSWindow.StyleMask(rawValue: 0xf)
            window.backingType = .buffered
            window.contentViewController = ViewController()
            window.setFrame(NSRect(x: 700, y: 200, width: 500, height: 500), display: false)
            windowController = NSWindowController()
            windowController.contentViewController = window.contentViewController
            windowController.window = window
            windowController.showWindow(self)
        }
        func applicationWillTerminate(_ aNotification: Notification) {
            // Insert code here to tear down your application
        }
    }

    import Cocoa

    class ViewController: NSViewController, DebugHelper {
        var myName: String = "ViewController"

        override func loadView() {
            view = NSView()
        }
    }
like image 26
Ron Avatar answered Sep 16 '22 12:09

Ron