16

我想以编程方式创建一个新的 NSWindow,但我找不到成功的方法。这个简单的代码不会显示新窗口。它出什么问题了?

@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)
    }
}
4

2 回答 2

17

Xcode 11 • 斯威夫特 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
    }
}

窗口样式蒙版

示例项目

于 2015-01-19T19:23:07.843 回答
1

这是一个没有任何花里胡哨的工作示例,已更新到 Swift 4.2。

它会根据您调用的函数创建一个带有和不带笔尖的裸窗口。

在 IB 中,到处取消选中“初始控制器”。要使 nib 版本正常工作,您必须为 IB 提供的窗口控制器提供“WindowController”的故事板 ID。

    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()
        }
    }
于 2019-02-24T20:21:47.470 回答