xcodeswiftuinavigationview

NavigationView .navigationBarBackButtonHidden not working on the first run


Hokahey,

I've tried setting navigationBarBackButtonHidden(true), but it doesn't work for me as expected. I'm trying to add a custom NavigationView Toolbar on the second screen. When I select the first Navigation Link and the second screen appears, the title is displayed as "Back Button Text." What the heck I didn't get?

first appearance of SecondScreen

Once you dismiss the second screen and select the navigation view again, the menu bar works as expected.

all following appearances of SecondScreen

Here is my code piece. Same behavior on Xcode 15.4 (latest beta patch)

struct MyNavigationView: View {
    
    var body: some View {
        NavigationView {
            ScrollView {
                NavigationLink {
                    SecondScreen(showCloseButton: true)
                        .navigationBarHidden(true)
                } label: {
                    HStack {
                        Image(systemName: "tortoise.circle.fill")
                        VStack(alignment: .leading, spacing: 0) {
                            Text("SecondScreen")
                                .font(.headline)
                            Text("Touch for Info")
                                .font(.system(size: 10))
                        }
                        Spacer()
                    }
                    
                }
            }
            .padding(.horizontal)
            .navigationBarItems(
                leading: Image(systemName: "trash.circle"),
                trailing:
                    NavigationLink(destination: {
                        SecondScreen(showCloseButton: false).navigationBarBackButtonHidden(true)
                    }, label: {
                        Image(systemName: "plus.circle")
                    })
                
            )
            .navigationTitle("Master View")
            .navigationBarBackButtonHidden(true)
            .accentColor(.pink)
        }
    }
}

struct SecondScreen: View {
    
    @Environment(\.dismiss) var dismiss
    var showCloseButton: Bool
    
    init(showCloseButton: Bool) {
        self.showCloseButton = showCloseButton
    }
    
    var body: some View {
        ZStack(alignment: .topLeading) {
            Color.pink
                .ignoresSafeArea()
            if showCloseButton {
                VStack {
                    Button {
                        dismiss()
                    } label: {
                        Image(systemName: "xmark.circle")
                            .font(.largeTitle)
                            .foregroundColor(.white)
                            .padding(20)
                    }
                }
            }
            VStack(alignment: .center) {
                Text("Hello world!").foregroundColor(.white).font(.largeTitle)
            }
            .frame(maxWidth: .infinity, maxHeight: .infinity)
            .toolbar {
                ToolbarItem(placement: .topBarLeading) {
                    Button(action: {
                        dismiss()
                    }, label: {
                        Image(systemName: "arrowshape.backward.circle")
                            .foregroundColor(.white)
                        Text("Back")
                            .foregroundColor(.white)
                            .fontWeight(.bold)
                    })
                }
            }
            //.toolbarBackground(.hidden, for: .navigationBar)
            .navigationBarBackButtonHidden(true)
            .navigationTitle("Detail Title")
        }
        
        
    }
}

As mentioned in the answers and comments below NavigationView is deprecated. The solution for this specific question is to change from NavigationView to NavigationViewStack, but of course the rest should also be adapted to new NavigationViewStack.


Solution

  • try to add the .navigationViewStyle(.stack) modifier to your NavigationView or use NavigationStack instead of NavigationView`