Skip to content

Instantly share code, notes, and snippets.

@ataias
Created February 13, 2021 11:38
Show Gist options
  • Select an option

  • Save ataias/5adcd6d917089c98c5d74481c2153e38 to your computer and use it in GitHub Desktop.

Select an option

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