TEAM LICENSES: Save money and learn new skills through a Hacking with Swift+ team license >>

SOLVED: How to add a button to a NavigationLink destination view

Forums > SwiftUI

I would like to add a button to the destination view of a NavigationLink but in order to add the button I have to make the destination view a NavigationView. When I do that I essentiallly am adding a NavigationView within a NavigationView resulting in the view title and button of the navigation view to be pushed down below the tool bar area.

How would I add a button without causing that effect? Code below which demonstrates what I am describing.

Any insights would be much appreciated.

import SwiftUI

struct ContentView: View {
    var body: some View {
        NavigationView {
            VStack(spacing: 25) {
                NavigationLink("Click for Detail View with Button", destination: DetailViewViewWithButton())
                    .navigationTitle("Main View")

                NavigationLink("Click for Detail View without Button", destination: DetailViewWithoutButton())
                    .navigationTitle("Main View")
            }
        }
    }
}

#Preview {
    ContentView()
}
import SwiftUI

struct DetailViewViewWithButton: View {
    var body: some View {
        NavigationView {
            Text("Details...")
                .navigationTitle("Detail View With Button")
                .navigationBarTitleDisplayMode(.inline)
                .toolbar {
                    Button("Save") {
                        // some code here...
                    }
                }
        }
    }
}

#Preview {
    DetailViewViewWithButton()
}
import SwiftUI

struct DetailViewWithoutButton: View {
    var body: some View {
        Text("Details...")
            .navigationTitle("Detail View Without Button")
            .navigationBarTitleDisplayMode(.inline)
    }
}

#Preview {
    DetailViewWithoutButton()
}

3      

Just remove NavigationView {} in that part of code:

struct DetailViewViewWithButton: View {
    var body: some View {
        NavigationView { // Remove that line
            Text("Details...")
                .navigationTitle("Detail View With Button")
                .navigationBarTitleDisplayMode(.inline)
                .toolbar {
                    Button("Save") {
                        // some code here...
                    }
                }
        } // and this line
    }
}

3      

Remove the NavigationView from the DetailViewViewWithButton

struct DetailViewViewWithButton: View {
    var body: some View {
//        NavigationView {
            Text("Details...")
                .navigationTitle("Detail View With Button")
                .navigationBarTitleDisplayMode(.inline)
                .toolbar {
                    Button("Save") {
                        // some code here...
                    }
                }
//        }
    }
}

PS if you want to see what looks like in preview

#Preview {
    NavigationStack {
        DetailViewViewWithButton()
    }
}

PS you should be using NavigationStack not NavigationView

3      

Hacking with Swift is sponsored by RevenueCat.

SPONSORED Take the pain out of configuring and testing your paywalls. RevenueCat's Paywalls allow you to remotely configure your entire paywall view without any code changes or app updates.

Learn more here

Sponsor Hacking with Swift and reach the world's largest Swift community!

Reply to this topic…

You need to create an account or log in to reply.

All interactions here are governed by our code of conduct.

 
Unknown user

You are not logged in

Log in or create account
 

Link copied to your pasteboard.