Skip to content

Instantly share code, notes, and snippets.

@lukewakeford
Last active November 24, 2016 10:57
Show Gist options
  • Save lukewakeford/5e6872e39404bcbccdc4a78b70b276a8 to your computer and use it in GitHub Desktop.
Save lukewakeford/5e6872e39404bcbccdc4a78b70b276a8 to your computer and use it in GitHub Desktop.
Toggle Hidden and Height Constraint
class ToggleHiddenLabel:UILabel {
var heightConstraint:NSLayoutConstraint!
var visible:Bool = false {
didSet {
if visible {
self.removeConstraint(self.heightConstraint)
self.hidden = false
} else {
self.addConstraint(self.heightConstraint)
self.hidden = true
}
}
}
override init(frame: CGRect) {
super.init(frame: frame)
self.heightConstraint = NSLayoutConstraint(item: self, attribute: .Height, relatedBy: .Equal, toItem: nil, attribute: .NotAnAttribute, multiplier: 0, constant: 0)
self.addConstraint(self.heightConstraint)
}
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
}
// Usage
let label = ToggleHiddenLabel()
label.visible = false
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment