Skip to content

Instantly share code, notes, and snippets.

@ataias
Created February 13, 2021 11:38
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 ataias/5adcd6d917089c98c5d74481c2153e38 to your computer and use it in GitHub Desktop.
Save ataias/5adcd6d917089c98c5d74481c2153e38 to your computer and use it in GitHub Desktop.
SwiftUI Modifier: Conditionally hide a view
import SwiftUI
struct ConditionalHide: ViewModifier {
var hidden: Bool
@ViewBuilder
func body(content: Content) -> some View {
if hidden {
content.hidden()
} else {
content
}
}
}
extension View {
func hidden(if hidden: Bool) -> some View {
self.modifier(ConditionalHide(hidden: hidden))
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment