Last active
May 2, 2018 21:32
-
-
Save MartinJNash/3839c3d99f0c5f6fdcda to your computer and use it in GitHub Desktop.
Xcode Snippets - NSView & UIView IBDesignable
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 | |
@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 | |
} | |
} |
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 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