Skip to content

Instantly share code, notes, and snippets.

@b3ll
Last active May 4, 2020 19:52
Show Gist options
  • Save b3ll/f6cf69dfa96212f2c1026f6cdaaa9383 to your computer and use it in GitHub Desktop.
Save b3ll/f6cf69dfa96212f2c1026f6cdaaa9383 to your computer and use it in GitHub Desktop.
why
//: A Cocoa based Playground to present user interface
import AppKit
import PlaygroundSupport
let nibFile = NSNib.Name("MyView")
var topLevelObjects : NSArray?
Bundle.main.loadNibNamed(nibFile, owner:nil, topLevelObjects: &topLevelObjects)
let views = (topLevelObjects as! Array<Any>).filter { $0 is NSView }
// Present the view in Playground
PlaygroundPage.current.liveView = views[0] as! NSView
class CoolView: NSView {
override init(frame frameRect: NSRect) {
super.init(frame: frameRect)
self.wantsLayer = true
self.layerContentsRedrawPolicy = .onSetNeedsDisplay
self.layer?.backgroundColor = .white
}
required init?(coder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
}
let v = CoolView(frame: .zero)
let bounds = NSRect(origin: .zero, size: CGSize(width: 150.0, height: 100.0))
v.bounds = bounds
v.setNeedsDisplay(bounds)
print(v.bounds)
v.frame = bounds
print(v.bounds)
let root = views[0] as! NSView
root.addSubview(v)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment