
To create a view that expands when a button is clicked in Swift, you can use a combination of SwiftUI and animations. Below is an example of how you can achieve this:
import SwiftUI
struct ExpandableView: View {
@State private var isExpanded = false
var body: some View {
VStack {
Button(action: {
withAnimation {
self.isExpanded.toggle()
}
}) {
Text("Toggle Expand")
.padding()
.foregroundColor(.white)
.background(Color.blue)
.cornerRadius(8)
}
if isExpanded {
Rectangle()
.foregroundColor(.green)
.frame(height: 200) // Initial height
.animation(.easeInOut) // Optional animation for expanding
}
}
.padding()
}
}
struct ContentView: View {
var body: some View {
ExpandableView()
}
}