use*_*978 10 macos xcode nswindow swift swift3
我想以编程方式创建一个新的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)
}
}
Run Code Online (Sandbox Code Playgroud)
Leo*_*bus 13
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
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
9803 次 |
| 最近记录: |