JakeW
JakeW

Reputation: 103

SwiftUI How to Remove "Back" Button from NavigationLink on next page?

Writing an app with a next page function at the bottom. The user clicks the next page and it takes them to the next page in the book. Currently when the "next page" button is hit, in the top left corner there is a "back" button on the new page that is added by Apple on default. Is there a way I can hide this button programmatically, so that way I can have my own back page button at the bottom?

My navigation link for the next page is below if that helps:

                    NavigationLink(destination: mostDangerous2()) {
                        Text("Next Page")
                            .font(.title3)
                            .fontWeight(.bold)
                            .padding(.all)
                            .overlay(
                                RoundedRectangle(cornerRadius: 15)
                                    .stroke(Color("maximumYellowRed"), lineWidth: 3)
                            )
                    }
                    .navigationBarBackButtonHidden(true)
                    .background(Color("gunMetal"))

Currently I have tried adding in .navigationBarBackButtonHidden(true) but this is not working. I have edited the asset color to match the background, but the button is still there and could potentially be clicked by a user - I feel like this is a sloppy solution. Any help appreciated

The back button I am talking about for reference

Upvotes: 0

Views: 1705

Answers (1)

Andrew Carter
Andrew Carter

Reputation: 334

You should hide the button in the detail view as suggested in the comments

import SwiftUI

struct DetailView: View {
    
    @Environment(\.presentationMode) var mode: Binding<PresentationMode>
    
    var body: some View {
        Button("Go back") {
            mode.wrappedValue.dismiss()
        }.navigationBarBackButtonHidden(true)
    }
    
}

struct ContentView: View {
    
    var body: some View {
        NavigationView {
            NavigationLink("Detail") {
                DetailView()
            }
        }
    }
}

struct ContentView_Previews: PreviewProvider {
    static var previews: some View {
        ContentView()
    }
}

Upvotes: 2

Related Questions