SwiftUI NavigationButton within navigationBarItems

Viewed 16092

I am looking to be able to use a NavigationButton to navigate to a new view within .navigationBarItems. This is how I expect it should work:

NavigationView {
    Text("Hello world")
    .navigationBarTitle(Text("Title"))
    .navigationBarItems(trailing:
        NavigationButton(destination: TestView()) {
            Text("Next")
        }
    )
}

However, the "Next" button doesn't do anything! I am aware of PresentationButton which provides a popover view like so:

NavigationView {
    Text("Hello world")
        .navigationBarTitle(Text("Title"))
        .navigationBarItems(trailing:
            PresentationButton(destination: TestView()) {
                Text("Next")
            }
        )
}

But this isn't what I'm looking for.

2 Answers

As I told you in comments, It was a bug. But it get fixed and it's working now exactly as you expected since Beta 5, But remember, NavigationButton has changed to NavigationLink. So it would be like:

struct ContentView: View {
    var body: some View{
        NavigationView {
            Text("Hello world")
            .navigationBarTitle(Text("Title"))
            .navigationBarItems(trailing:
                NavigationLink(destination: TestView()) {
                    Text("Next")
                }
            )
        }
    }
}

If you're having a list & you need to navigate through the screens then you should use NavigationLink instead of NavigationButton cause it's changed recently. For example:-

NavigationView{
        
        List(landmarkData) { landmark in
            
            NavigationLink(destination: LandmarkDetail()){
                
                LandmarkRow(landmark: landmark)
            }
                
        }
        
}
Related