Created
June 20, 2020 09:36
-
-
Save vlondon/e14c146da4ffc3b2882bb72dfcb72881 to your computer and use it in GitHub Desktop.
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
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