Skip to content

Instantly share code, notes, and snippets.

@h2ero
Last active November 19, 2017 03:45
Show Gist options
  • Save h2ero/5d059b116848f73190f27ce7f1f958cb to your computer and use it in GitHub Desktop.
Save h2ero/5d059b116848f73190f27ce7f1f958cb to your computer and use it in GitHub Desktop.
//: [Previous](@previous)
import AppKit
import XCPlayground
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.active = 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
view.layer?.backgroundColor = NSColor.windowBackgroundColor().CGColor
class test: NSObject {
@objc func test(sender: AnyObject) {
print("Hallo")
}
}
let t = test()
var button: NSButton = {
$0.title = "Hallo"
$0.target = t
$0.action = #selector(test.test(_:))
$0.enabled = true
$0.bezelStyle = .RoundedBezelStyle
return $0
}(NSButton(frame: NSRect(x: 0, y: 0, width: 100, height: 30)))
view.addSubview(button)
XCPlaygroundPage.currentPage.liveView = view
NSLayoutConstraint(view: button, to: view, attribute: .CenterX)
NSLayoutConstraint(view: button, to: view, attribute: .CenterY)
@eonist
Copy link

eonist commented Jan 18, 2017

Hey, awesome example!

Forked it and updated it to swift 3.0

https://gist.github.com/eonist/75b752a5abcd472e6781d6a0c12f6919

@h2ero
Copy link
Author

h2ero commented Nov 19, 2017

@eonist Good job

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment