Skip to content

Instantly share code, notes, and snippets.

@sanzaru
Last active February 15, 2024 15:26
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save sanzaru/83a6dc8d8c93f267d4a1a258a7a92329 to your computer and use it in GitHub Desktop.
Save sanzaru/83a6dc8d8c93f267d4a1a258a7a92329 to your computer and use it in GitHub Desktop.
SwiftUI collapsible view
import SwiftUI
struct Collapsible<Content: View>: View {
@State var label: () -> Text
@State var content: () -> Content
@State private var collapsed: Bool = true
var body: some View {
VStack {
Button(
action: { self.collapsed.toggle() },
label: {
HStack {
self.label()
Spacer()
Image(systemName: self.collapsed ? "chevron.down" : "chevron.up")
}
.padding(.bottom, 1)
.background(Color.white.opacity(0.01))
}
)
.buttonStyle(PlainButtonStyle())
VStack {
self.content()
}
.frame(minWidth: 0, maxWidth: .infinity, minHeight: 0, maxHeight: collapsed ? 0 : .none)
.clipped()
.animation(.easeOut)
.transition(.slide)
}
}
}
@BeneVico
Copy link

Works really well, thank you! Did some minor adjustments like passing the collapsed boolean as a Binding.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment