Skip to content

Instantly share code, notes, and snippets.

@vlondon
Created June 20, 2020 09:36
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save vlondon/e14c146da4ffc3b2882bb72dfcb72881 to your computer and use it in GitHub Desktop.
Save vlondon/e14c146da4ffc3b2882bb72dfcb72881 to your computer and use it in GitHub Desktop.
import SwiftUI
import PlaygroundSupport
struct EmptyModifier: ViewModifier {
let isEmpty: Bool
func body(content: Content) -> some View {
// We have to group the views or we will get an error:
// "Function declares an opaque return type, but has no return statements in its body from which to infer an underlying type"
Group {
if isEmpty {
EmptyView()
} else {
content
}
}
}
}
extension View {
/// Whether the view should be empty.
/// - Parameter bool: Set to `true` to hide the view (return EmptyView instead). Set to `false` to show the view.
func isEmpty(_ bool: Bool) -> some View {
modifier(EmptyModifier(isEmpty: bool))
}
}
struct ContentView: View {
@State private var isSubtitleVisible: Bool = false
var body: some View {
VStack {
Text("Title")
Text("Subtitle").isEmpty(!isSubtitleVisible)
Text("Body")
Spacer()
}
}
}
PlaygroundPage.current.setLiveView(ContentView())
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment