Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can't do a Simple Navigate to View and back SwiftUI Navigation Bar Button

Tags:

xcode

swiftui

I'm trying to do a simple SwiftUI navigation from one view to another and back using a bar button item. I have tried three different approaches to calling a new view. Using a Button in the body view works, but using NavigationBarItems in the navigation bar fails in two different ways.

Here's the start view:

struct ContentView: View {
    @State private var showSecondView = false
    var body: some View {
        NavigationView {
            VStack {
                Text("This is the content view")
                    .navigationBarTitle("Nav Title")
//this works ONCE only:
                    .navigationBarItems(trailing: Button(action: {self.showSecondView.toggle()}) {
                        Text("SecondView")
                    })
//this always fails on return to contentview with error:
//Tried to pop to a view controller that doesn't exist
//                .navigationBarItems(trailing:
//                    NavigationLink(destination: SecondView()) {
//                        Text("SecondNav")
//                    }
//                )
//This always works:
                Button(action: {self.showSecondView.toggle()} ) {
                    Text("Call Modal Second View")
                }.padding()
                Text(self.showSecondView ? "true" : "false")
            }.sheet(isPresented: $showSecondView) {
                SecondView()
            }
        }
    }
}

If I use a NavigationLink in the NavigationBarItems, the SecondView is displayed, but on return to the ContentView, it crashes with the error: "Tried to pop to a view controller that doesn't exist"

If I use a Button in the NavigationBarItems, the transition to the SecondView works once and only once. The return to ContentView works but the button no longer functions. Interestingly, If the first action taken is with the Button in the Body, the NavigationBarItem does not work even once.

And the simple SecondView:

struct SecondView: View {
    @Environment(\.presentationMode) var presentation
    var body: some View {
        NavigationView {
            VStack{
                Text("This is the second view")
                Button(action: { self.presentation.wrappedValue.dismiss()}) {
                    Text("Dismiss Modal")
                }.padding()
            }
        }
    }
}

I'm confused. Any guidance would be appreciated. Xcode 11.2 (11B44), Catalina 10.15

like image 848
JohnSF Avatar asked Oct 17 '19 04:10

JohnSF


People also ask

How do I access the navigation bar in SwiftUI?

To show a Navigation Bar using SwiftUI, we should use the NavigationView component that is responsible for this purpose. It requires that we provide the Content that is a View type. The Content can be anything from a text field to scrollable content. In short, it can be any SwiftUI view.

How do I navigate from one view to another in SwiftUI?

The first step is to create a mother view that hosts both Content Views as its subviews. For this purpose, create a new File-New-File-SwiftUI View and name it MotherView. In this view, we want to show either ContentViewA or ContentViewB depending on where the user navigated to.

How do I customize my navigation bar in SwiftUI?

To change a navigation bar color in SwiftUI, you apply toolbarBackground modifier to the content view of NavigationStack . NavigationView is deprecated in iOS 16. toolbarBackground accepts two parameters. ShapeStyle : The style to display as the background of the bar.


2 Answers

This is still an issue for me, I am having the same issue with popover (Modal) presentation and pushing Second controller via NavigationLink in navigationBarItems.

This is a really serious bug. The only way it works correctly is when the NavigationLink is inside NavigationView content and not navigationBarItems.

This is a really breaking as NavigationBar buttons are suppose to work that way.

like image 195
Jan Kubny Avatar answered Oct 01 '22 21:10

Jan Kubny


I came across this issue today when I updated my Xcode to 11.2. According to this post it seems to be a bug with 13.2. I tested it on my actual iPhone X, which is still running 13.1.2, and it works just fine there.

like image 21
machinehead115 Avatar answered Oct 01 '22 20:10

machinehead115