0

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

JakeW
  • 103
  • 1
  • 1
  • 4
  • Similar [question](https://stackoverflow.com/questions/59419181/swiftui-navigationbarbackbuttonhidden-not-working-as-expected), but the layout is different than yours. – Raptor Nov 22 '22 at 03:59
  • Thank you, but I tried the solution (remove an extraneous NavigationView) but this did not work for me - I only have one NavigationView and removing it broke my NavigationLink. – JakeW Nov 22 '22 at 04:02
  • 2
    What happens if you add `.navigationBarBackButtonHidden(true)` to your `destination` instead of the `NavigationLink`? – jnpdx Nov 22 '22 at 04:24
  • Wow, I can't believe I didn't think of that. This fixed it, thank you! – JakeW Nov 22 '22 at 04:53

1 Answers1

1

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()
    }
}