xcode 以编程方式创建 NSWindow

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/28031732/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 06:30:43  来源:igfitidea点击:

Programmatically create NSWindow

swiftxcodemacosswift3nswindow

提问by user3345978

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?

我想以编程方式创建一个新的 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)
    }
}

回答by Leo Dabus

Xcode 11 ? Swift 5.1

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

Window Style Mask

窗口样式遮罩

Sample project

示例项目

回答by Ron

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

这是一个没有任何花里胡哨的工作示例,已更新到 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."

在 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()
        }
    }