Home > Enterprise >  Creating a custom back button with NavigationView on SwiftUI
Creating a custom back button with NavigationView on SwiftUI

Time:12-03

I'm trying to create a custom back button on SwiftUI, but I can't figure out how to do it.

The idea is to hide the "Back" button at the top left that provides NavigationView, and make a custom button with the same functionality.

struct AnadirDatosViewA: View {
    @Environment(\.presentationMode) var presentation
    
    var body: some View{
        NavigationView(){
            Color(red: 48 / 255, green: 49 / 255, blue: 54 / 255)
                .edgesIgnoringSafeArea(.all)
                .overlay(
                    VStack{
                        AnadirDatosExpB()
                        
                        HStack{
                            
                            NavigationLink(destination:NuevoExperimentoView()){
                                Text("Back") //HERE
                                
                                NavigationLink(destination:AnadirDatosExpA()){
                                    Text("Next")
                                        
                                }
                            }
                        }
                    }
                )
        }.navigationBarBackButtonHidden(true)
    }
}

Right now I'm "cheating" by using the view I want go go back as destination, but it doesn't work the same...

What can I do?

CodePudding user response:

You can use the presentationMode var from the environment inside a Button:

See the commented example below for a possible implementation.

struct ContentView: View{
    
    var body: some View{
        // I am navigating with a Navigationlink, so there is
        // no need for it in the AnadirDatosViewA
        NavigationView {
            NavigationLink("show AnadirDatosViewA") {
                AnadirDatosViewA()
            }
        }
    }
}


struct AnadirDatosViewA: View {
    @Environment(\.presentationMode) var presentation
    
    var body: some View{
        // if you navigated to this by a Navigationlink remove the NavigationView
        Color(red: 48 / 255, green: 49 / 255, blue: 54 / 255)
            .edgesIgnoringSafeArea(.all)
            .overlay(
                HStack{
                    // This Button will dismiss the View
                    Button("Back"){
                        // with help of th presentationMode from the environment
                        presentation.wrappedValue.dismiss()
                    }
                    // This NavigationLink can forward you to another view
                    NavigationLink("Next") {
                        TextView(text: "last")
                    }
                }
                // This will hide the back Button in this View
            ).navigationBarBackButtonHidden(true)
    }
}
// HelperView
struct TextView: View{
    var text: String
    var body: some View{
        Text(text)
    }
}
  • Related