Last active
January 11, 2020 20:51
NSWindow without Storyboard - https://kicsipixel.github.io/2020/nostoryboard/
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
private var window: NSWindow? | |
func applicationDidFinishLaunching(_ aNotification: Notification) { | |
let windowSize = NSSize(width: 480, height: 240) | |
let screenSize = NSScreen.main?.frame.size ?? .zero | |
let rect = NSMakeRect(screenSize.width/2 - windowSize.width/2, screenSize.height/2 - windowSize.height/2, windowSize.width, windowSize.height) | |
window = NSWindow(contentRect: rect, styleMask: [.miniaturizable, .closable, .resizable, .titled], backing: .buffered, defer: false) | |
window?.title = "No Storyboard Window" | |
window?.makeKeyAndOrderFront(nil) | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// AppDelegate.swift | |
// nostoryboard | |
// | |
// Created by Szabolcs Toth on 11/01/2020. | |
// Copyright © 2020 purzelbaum.hu. All rights reserved. | |
// | |
import Cocoa | |
// @NSApplicationMain | |
class AppDelegate: NSObject, NSApplicationDelegate { | |
private var window: NSWindow? | |
func applicationDidFinishLaunching(_ aNotification: Notification) { | |
let windowSize = NSSize(width: 480, height: 240) | |
let screenSize = NSScreen.main?.frame.size ?? .zero | |
let rect = NSMakeRect(screenSize.width/2 - windowSize.width/2, screenSize.height/2 - windowSize.height/2, windowSize.width, windowSize.height) | |
window = NSWindow(contentRect: rect, styleMask: [.miniaturizable, .closable, .resizable, .titled], backing: .buffered, defer: false) | |
window?.title = "No Storyboard Window" | |
window?.contentViewController = MyViewController() | |
window?.makeKeyAndOrderFront(nil) | |
// Get application name | |
let bundleInfoDict: NSDictionary = Bundle.main.infoDictionary! as NSDictionary | |
let appName = bundleInfoDict["CFBundleName"] as! String | |
// Add menu | |
let mainMenu = NSMenu() | |
let mainMenuFileItem = NSMenuItem(title: "File", action:nil, keyEquivalent: "") | |
let fileMenu = NSMenu(title: "File") | |
fileMenu.addItem(withTitle: "About \(appName)", action: nil, keyEquivalent: "") | |
fileMenu.addItem(NSMenuItem.separator()) | |
fileMenu.addItem(withTitle: "Hide \(appName)", action: #selector(NSApplication.hide(_:)), keyEquivalent: "") | |
fileMenu.addItem(withTitle: "Hide Others", action: #selector(NSApplication.hideOtherApplications(_:)), keyEquivalent: "") | |
fileMenu.addItem(withTitle: "Show All", action: #selector(NSApplication.unhideAllApplications(_:)), keyEquivalent: "") | |
fileMenu.addItem(NSMenuItem.separator()) | |
fileMenu.addItem(withTitle: "Quit \(appName)", action: #selector(NSApplication.terminate(_:)), keyEquivalent: "q") | |
mainMenuFileItem.submenu = fileMenu | |
mainMenu.addItem(mainMenuFileItem) | |
NSApp.mainMenu = mainMenu | |
} | |
func applicationWillTerminate(_ aNotification: Notification) { | |
// Insert code here to tear down your application | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// MyViewController.swift | |
// nostoryboard | |
// | |
// Created by Szabolcs Toth on 11/01/2020. | |
// Copyright © 2020 purzelbaum.hu. All rights reserved. | |
// | |
import Cocoa | |
class MyViewController: NSViewController { | |
override func loadView() { | |
let rect = NSRect(x: 0, y: 0, width: 480, height: 240) | |
view = NSView(frame: rect) | |
view.wantsLayer = true | |
view.layer?.backgroundColor = NSColor.white.cgColor | |
let label = NSTextField() | |
label.frame = CGRect(x: view.bounds.width/2-50, y: view.bounds.height/2-22, width: 100, height: 44) | |
label.stringValue = "Hello, World!" | |
label.backgroundColor = .white | |
label.isBezeled = false | |
label.isEditable = false | |
view.addSubview(label) | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
let label = NSTextField() | |
label.frame = CGRect(x: view.bounds.width/2-50, y: view.bounds.height/2-22, width: 100, height: 44) | |
label.stringValue = "Hello, World!" | |
label.backgroundColor = .white | |
label.isBezeled = false | |
label.isEditable = false | |
view.addSubview(label) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import Cocoa | |
let delegate = AppDelegate() | |
NSApplication.shared.delegate = delegate | |
_ = NSApplicationMain(CommandLine.argc, CommandLine.unsafeArgv) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
override func loadView() { | |
let rect = NSRect(x: 0, y: 0, width: 480, height: 240) | |
view = NSView(frame: rect) | |
view.wantsLayer = true | |
view.layer?.backgroundColor = NSColor.white.cgColor | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment