SwiftUI changing text title of navigationBar Button at the current view to "Back" instead of inheriting the text from previous view title

Viewed 193

I have a swiftUI view with navigation links that when i click will navigate to another view . The issue is the second view navigationBa button title still has the title of the previous view instead of a logical back title . How i can have the title as Back with changing the title as "Back" in the first view .

First view navigationBar code: The second view just shows the news website in a WebView.

.navigationBarTitle("Breaking News")

The way i tried is changing the title to this:

.navigationBarTitle("Back")

This will work but the title of the first view changes to "Back" Instead of "Breaking News" Is there any way i can fix this

1 Answers

An alternative approach is to hide the back button and create your own back button like this:

struct ContentView: View {
    var body: some View {
        NavigationView {
                NavigationLink(destination : SomeView()) {
                    Text("Open")
            }
            .navigationBarTitle("Breaking News")
        }
    }
}

// Use navigationBarItems for creating your own bar item.

struct SomeView : View {
    @Environment(\.presentationMode) var mode
    var body : some View {
        Text("Hello, World!")
            .navigationBarBackButtonHidden(true)
            .navigationBarItems(leading:
            Button(action : {
                self.mode.wrappedValue.dismiss()
            }){
            Text("\(Image(systemName: "chevron.left"))Back")
        })
    }
}
Related