Skip to content

Instantly share code, notes, and snippets.

@ipodishima
Forked from sanzaru/Collapsible.swift
Last active May 8, 2025 05:54
Show Gist options
  • Save ipodishima/1eff23ce12d0d8cb55a44b193ba4145d to your computer and use it in GitHub Desktop.
Save ipodishima/1eff23ce12d0d8cb55a44b193ba4145d 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)
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment