Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to wrap a button in a navigation link in SwiftUI?

I am new to swift, and I am trying to place a "quick exit" button on each content page of my app, the button itself is how I want it, but I am having a hard time getting the actual navigation to work. It builds, but simply does nothing when clicked.

struct ContentMenu: View {
    var body: some View {
        ZStack{
            VStack {
                Spacer()
                HStack {
                    Spacer()
                    NavigationLink(destination: ContentView2())
                    {
                        Button(action:{} ){
                            Image(systemName: "figure.run.circle.fill")
                                .resizable(resizingMode: .stretch)
                                .font(.largeTitle)
                                .frame(width: 65, height: 65)
                                .background(Color .red)
                                .clipShape(Circle())
                            .foregroundColor(Color .black)}
                        
                    }.padding()
                        .shadow(radius: 5)
                }
            }
        }
    }
}

>Solution :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

In SwiftUI, the NavigationLink itself is a special type of button. So you absolutely do not need to wrap a button inside a NavigationLink. Simply do:

NavigationLink(destination: ContentView2()) {
    Image(systemName: "figure.run.circle.fill")
        .resizable(resizingMode: .stretch)
        .font(.largeTitle)
        .frame(width: 65, height: 65)
        .background(Color .red)
        .clipShape(Circle())
        .foregroundColor(Color .black)}
}.padding()
.shadow(radius: 5)

Don’t forget to wrap your view inside a NavigationStack or a NavigationView (if you plan to support iOS 15-) in order for the navigation to work.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading