Skip to content

Instantly share code, notes, and snippets.

@MartinJNash
Last active May 2, 2018 21:32
Show Gist options
  • Star 5 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save MartinJNash/3839c3d99f0c5f6fdcda to your computer and use it in GitHub Desktop.
Save MartinJNash/3839c3d99f0c5f6fdcda to your computer and use it in GitHub Desktop.
Xcode Snippets - NSView & UIView IBDesignable
import Cocoa
@IBDesignable
class <#ClassName#> : NSView {
override init(frame frameRect: NSRect) {
super.init(frame: frameRect)
commonSetup()
}
required init?(coder: NSCoder) {
super.init(coder: coder)
commonSetup()
}
override init() {
super.init()
commonSetup()
}
private func commonSetup() {
wantsLayer = true
}
override func prepareForInterfaceBuilder() {
}
override var wantsUpdateLayer: Bool {
return true
}
override func updateLayer() {
updateConstraints()
}
private var layoutOnceToken: dispatch_once_t = 0
override func updateConstraints() {
dispatch_once(&layoutOnceToken, {
// one time only layout code
})
super.updateConstraints()
// adjust layout here
}
}
import UIKit
@IBDesignable
class <#ClassName#>: UIView {
required init(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
commonSetup()
}
override init(frame: CGRect) {
super.init(frame: frame)
commonSetup()
}
override init() {
super.init(frame: CGRectZero)
commonSetup()
}
override func prepareForInterfaceBuilder() {
updateConstraints()
commonSetup()
}
private func commonSetup() {
}
private var layoutOnceToken: dispatch_once_t = 0
override func updateConstraints() {
dispatch_once(&layoutOnceToken, {
// one time only layout code
})
super.updateConstraints()
// adjust layout here
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment