Created
May 11, 2022 04:56
-
-
Save kiarashvosough1999/10b0aed213ca34799170e2885b642be3 to your computer and use it in GitHub Desktop.
Expandable List SwifUI
This file contains 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
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