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
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.
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.
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.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With