Skip to content

Instantly share code, notes, and snippets.

@noahsark769
Last active February 3, 2019 03:46
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save noahsark769/86ba7acb8e77d403b2d83754c2ec7aeb to your computer and use it in GitHub Desktop.
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
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