Search code examples
animationviewsizeswiftui

SwiftUI - Animate resize of a view frame


How do you animate the size of a view, such that the view may grow or shrink using the frame height? I need to transition between two known dimensions.


Solution

  • I don't know exactly what you need but here is a very basic example with a Rectangle that gets scaled when you tap the Button:

    struct ContentView: View {
    
        @State var animate = false
    
        var body: some View {
            VStack {
                Button(action: {
                    withAnimation {
                        self.animate.toggle()
                    }
                }, label: {
                    Text("Animate")
                })
                Rectangle()
                    .foregroundColor(.blue)
                    .frame(width: self.animate ? 100 : 150, height: self.animate ? 60 : 90)
            }
        }
    }
    

    Please add some code to your next question or edit the question so people can provide a more specific answer.