Search code examples
iosswiftswiftui

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.


Solution

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