在Swift中以编程方式为OS X构建UI(不使用Interface Builder)

时间:2015-01-20 06:52:56

标签: macos swift appcode

我是Swift的新手,我想使用AppCode开发一个OS X应用程序(我不想使用XCode),也没有使用故事板。所以我需要构建UI并以编程方式管理出口和操作。 我正在寻找一个示例代码/教程视频的链接,用于“Hello World”或以编程方式创建UI的示例应用程序。感谢帮助。感谢

1 个答案:

答案 0 :(得分:0)

import Cocoa

@NSApplicationMain
class AppDelegate: NSObject, NSApplicationDelegate {
    let newWindow = NSWindow(contentRect: NSMakeRect(0, 0, NSScreen.mainScreen()!.frame.width/2, NSScreen.mainScreen()!.frame.height/2), styleMask: NSTitledWindowMask|NSResizableWindowMask|NSMiniaturizableWindowMask|NSClosableWindowMask, backing: NSBackingStoreType.Buffered, defer: false)
    let newText = NSTextField(frame: NSMakeRect(0, NSScreen.mainScreen()!.frame.height/4, NSScreen.mainScreen()!.frame.width/2, 40))
    let myView = NSView(frame: NSMakeRect(0, 0, NSScreen.mainScreen()!.frame.width/2, NSScreen.mainScreen()!.frame.height/2))

    func applicationDidFinishLaunching(aNotification: NSNotification) {
        // Insert code here to initialize your application
        newText.font = NSFont(name: "Arial Black", size: 24)
        newText.backgroundColor = NSColor.clearColor()
        newText.bordered = false
        newText.textColor = NSColor.whiteColor()
        newText.alignment = .CenterTextAlignment
        newText.stringValue = "Hello World"
        newText.selectable = false
        newWindow.opaque = false
        newWindow.movableByWindowBackground = true
        newWindow.backgroundColor = NSColor(calibratedHue: 0, saturation: 1.0, brightness: 0, alpha: 0.7)
        newWindow.title = "Hello World"
        newWindow.center()
        newWindow.contentView.addSubview(myView)
        myView.addSubview(newText)
        newWindow.makeKeyAndOrderFront(nil)
    }

    func applicationWillTerminate(aNotification: NSNotification) {
        // Insert code here to tear down your application
    }
}