Skip to content

Instantly share code, notes, and snippets.

@kiarashvosough1999
Created May 11, 2022 04:56
Show Gist options
  • Save kiarashvosough1999/10b0aed213ca34799170e2885b642be3 to your computer and use it in GitHub Desktop.
Save kiarashvosough1999/10b0aed213ca34799170e2885b642be3 to your computer and use it in GitHub Desktop.
Expandable List SwifUI
struct ContentView: View {
@State var isExpanded = false
@State var subviewHeight : CGFloat = 0
var body: some View {
VStack {
Text("Headline")
VStack {
Text("More Info")
Text("And more")
Text("And more")
Text("And more")
Text("And more")
Text("And more")
}
}
.background(GeometryReader {
Color.clear.preference(key: ViewHeightKey.self,
value: $0.frame(in: .local).size.height)
})
.onPreferenceChange(ViewHeightKey.self) { subviewHeight = $0 }
.frame(height: isExpanded ? subviewHeight : 50, alignment: .top)
.padding()
.clipped()
.frame(maxWidth: .infinity)
.transition(.move(edge: .bottom))
.background(Color.gray.cornerRadius(10.0))
.onTapGesture {
withAnimation(.easeIn(duration: 2.0)) {
isExpanded.toggle()
}
}
}
}
struct ViewHeightKey: PreferenceKey {
static var defaultValue: CGFloat { 0 }
static func reduce(value: inout Value, nextValue: () -> Value) {
value = value + nextValue()
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment