Last active
February 3, 2019 03:46
-
-
Save noahsark769/86ba7acb8e77d403b2d83754c2ec7aeb to your computer and use it in GitHub Desktop.
UIView subclass which displays one of a given set of views at a time
This file contains hidden or 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
final class EitherView: UIView { | |
private let views: [UIView] | |
init(views: [UIView]) { | |
self.views = views | |
super.init(frame: .zero) | |
} | |
required init?(coder aDecoder: NSCoder) { | |
fatalError("init(coder:) has not been implemented") | |
} | |
func setEnabled(_ viewToEnable: UIView) { | |
for knownView in views { | |
if knownView == viewToEnable { | |
viewToEnable.removeFromSuperview() | |
addSubview(viewToEnable) | |
viewToEnable.translatesAutoresizingMaskIntoConstraints = false | |
viewToEnable.leadingAnchor.constraint(equalTo: self.leadingAnchor).isActive = true | |
viewToEnable.trailingAnchor.constraint(equalTo: self.trailingAnchor).isActive = true | |
viewToEnable.topAnchor.constraint(equalTo: self.topAnchor).isActive = true | |
viewToEnable.bottomAnchor.constraint(equalTo: self.bottomAnchor).isActive = true | |
} else { | |
knownView.removeFromSuperview() | |
} | |
} | |
self.setNeedsUpdateConstraints() | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment