Created
June 30, 2022 18:48
-
-
Save kiarashvosough1999/1d6dff256357f589e218bf9d934a6352 to your computer and use it in GitHub Desktop.
Expandable View
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