How do I navigate programmatically in SwiftUI?

In SwiftUI, you can navigate programmatically using the NavigationLink and a state variable to control the navigation. This allows you to push new views onto the navigation stack based on certain actions or conditions in your app.

struct ContentView: View {
        @State private var isActive = false

        var body: some View {
            NavigationView {
                VStack {
                    NavigationLink(destination: SecondView(), isActive: $isActive) {
                        EmptyView()
                    }
                    Button("Navigate to Second View") {
                        isActive = true
                    }
                }
            }
        }
    }

    struct SecondView: View {
        var body: some View {
            Text("Welcome to the Second View!")
        }
    }

SwiftUI Navigation Programmatic Navigation iOS Development