Last active
November 24, 2016 10:57
-
-
Save lukewakeford/5e6872e39404bcbccdc4a78b70b276a8 to your computer and use it in GitHub Desktop.
Toggle Hidden and Height Constraint
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
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