Swiftui Navigationbutton Within navigationBarItems

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.

3

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")
                }
            )
        }
    }
}
1

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)
            }
                
        }
        
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Chloe Bennett

Chloe Bennett

Culture, Media & Entertainment Columnist

Chloe Bennett explores the intersection of pop culture, streaming entertainment, digital trends, and contemporary lifestyle. Her weekly commentary reaches thousands of culture enthusiasts.

Share this article
Twitter Facebook Pinterest