Home > database >  How to create a window in Swift Mac
How to create a window in Swift Mac

Time:08-03

I cannot find a simple answer to create a window in swift without using a class or another fancy method. This is the best result so far:

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
    }
}

Is there a way to do this without the use of a class? Thanks.

CodePudding user response:

Use Objective-C instead. The problems that you are having are all solvable.

  • Related