Crear NSWindow programáticamente en Swift
xcode macos (1)
import Cocoa
@NSApplicationMain
class AppDelegate: NSObject, NSApplicationDelegate {
let newWindow = NSWindow(contentRect: NSMakeRect(0, 0, NSScreen.main()!.frame.midX, 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(_ notification: Notification) {
createNewWindow()
}
func applicationWillTerminate(_ notification: Notification) {
// Insert code here to tear down your application
}
}
Quiero crear una nueva ventana mediante programación usando Swift pero no pude encontrar una manera de tener éxito. Este código simple no muestra una nueva ventana. ¿Qué pasa con mi código?
@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)
}
}