Skip to content

Instantly share code, notes, and snippets.

@carlynorama
Last active July 16, 2022 18:32
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 carlynorama/baaceac7e64fa6ee22385277268858f8 to your computer and use it in GitHub Desktop.
Save carlynorama/baaceac7e64fa6ee22385277268858f8 to your computer and use it in GitHub Desktop.
A container that collapses.
//Based off of https://rensbr.eu/blog/swiftui-escaping-closures/
struct CollapsableView<Content: View>: View {
let content: Content
@Binding var isVisible:Bool
init(value:Binding<Bool>, @ViewBuilder content: () -> Content) {
self.content = content()
self._isVisible = value
}
var body: some View {
VStack {
if isVisible {
content
}
Button(isVisible ? "↓ open" : "↑ close") {
isVisible.toggle()
}
}
}
}
struct ContentView: View {
@State var seeTheThing = true
var body: some View {
CollapsableView(value: $seeTheThing) {
Text("Hi!")
}
}
}
struct ContentView_Previews: PreviewProvider {
static var previews: some View {
ContentView()
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment