OS Swift 中的 X 应用程序没有 XIB
OS X app in Swift without XIB
有没有人想出一种方法来 bootstrap 不使用 XIB(或根本不使用 IB)的基于 NSWindow 的应用程序?在iOS中,这很简单;
func application(application: UIApplication!, didFinishLaunchingWithOptions launchOptions: NSDictionary!) -> Bool {
window = UIWindow(frame: UIScreen.mainScreen().bounds)
window!.rootViewController = ViewController(nibName: nil, bundle: nil)
window!.makeKeyAndVisible()
window!.backgroundColor = UIColor.whiteColor()
return true
}
但我就是无法让它在桌面应用程序中正常工作。NSWindow 没有 makeKeyAndVisible() 方法,而 NSApplicationDelegate 似乎有十几个 func application() 覆盖。这是什么魔法咒语?
import Cocoa
@NSApplicationMain
class AppDelegate: NSObject, NSApplicationDelegate {
@IBOutlet weak var window: NSWindow!
let newWindow = NSWindow(contentRect: NSScreen.mainScreen()!.frame, styleMask: NSBorderlessWindowMask, backing: NSBackingStoreType.Buffered, defer: false)
func applicationDidFinishLaunching(aNotification: NSNotification) {
// Insert code here to initialize your application
newWindow.opaque = false
newWindow.movableByWindowBackground = true
newWindow.backgroundColor = NSColor.whiteColor()
newWindow.makeKeyAndOrderFront(nil)
}
func applicationWillTerminate(aNotification: NSNotification) {
// Insert code here to tear down your application
}
}
有没有人想出一种方法来 bootstrap 不使用 XIB(或根本不使用 IB)的基于 NSWindow 的应用程序?在iOS中,这很简单;
func application(application: UIApplication!, didFinishLaunchingWithOptions launchOptions: NSDictionary!) -> Bool {
window = UIWindow(frame: UIScreen.mainScreen().bounds)
window!.rootViewController = ViewController(nibName: nil, bundle: nil)
window!.makeKeyAndVisible()
window!.backgroundColor = UIColor.whiteColor()
return true
}
但我就是无法让它在桌面应用程序中正常工作。NSWindow 没有 makeKeyAndVisible() 方法,而 NSApplicationDelegate 似乎有十几个 func application() 覆盖。这是什么魔法咒语?
import Cocoa
@NSApplicationMain
class AppDelegate: NSObject, NSApplicationDelegate {
@IBOutlet weak var window: NSWindow!
let newWindow = NSWindow(contentRect: NSScreen.mainScreen()!.frame, styleMask: NSBorderlessWindowMask, backing: NSBackingStoreType.Buffered, defer: false)
func applicationDidFinishLaunching(aNotification: NSNotification) {
// Insert code here to initialize your application
newWindow.opaque = false
newWindow.movableByWindowBackground = true
newWindow.backgroundColor = NSColor.whiteColor()
newWindow.makeKeyAndOrderFront(nil)
}
func applicationWillTerminate(aNotification: NSNotification) {
// Insert code here to tear down your application
}
}