Skip to content

Instantly share code, notes, and snippets.

@eonist
Forked from h2ero/playground.swift
Last active January 18, 2017 13:09
Show Gist options
  • Save eonist/75b752a5abcd472e6781d6a0c12f6919 to your computer and use it in GitHub Desktop.
Save eonist/75b752a5abcd472e6781d6a0c12f6919 to your computer and use it in GitHub Desktop.
InteractivePlayground with button that prints hello (swift 3.0.1)
import AppKit
import XCPlayground
import Cocoa
import PlaygroundSupport
extension NSLayoutConstraint {
convenience init(view item: NSView, to toItem: NSView, attribute: NSLayoutAttribute, constant c: CGFloat = 0) {
self.init(item: item, attribute: attribute,
relatedBy: .equal,
toItem: toItem, attribute: attribute,
multiplier: 1.0, constant: c)
self.isActive = true
self.shouldBeArchived = true
item.translatesAutoresizingMaskIntoConstraints = false
}
}
let viewRect = NSRect(x: 0, y: 0, width: 300, height: 200)
var view = NSView(frame: viewRect)
view.wantsLayer = true
class Test: NSObject {
@objc func test(sender: AnyObject) {
Swift.print("Hallo")
}
}
let t = Test()
var button: NSButton = {
$0.title = "Hallo"
$0.target = t
$0.action = #selector(t.test)
$0.isEnabled = true
$0.bezelStyle = .rounded
return $0
}(NSButton(frame: NSRect(x: 0, y: 0, width: 100, height: 30)))
view.addSubview(button)
PlaygroundPage.current.liveView = view
//PlaygroundPage.current.needsIndefiniteExecution = true
NSLayoutConstraint(view: button, to: view, attribute: .centerX)
NSLayoutConstraint(view: button, to: view, attribute: .centerY)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment