Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SwiftUI NavigationButton within navigationBarItems

Tags:

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.

like image 389
David Southgate Avatar asked Jun 14 '19 23:06

David Southgate


People also ask

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.


1 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")
                }
            )
        }
    }
}
like image 122
Mojtaba Hosseini Avatar answered Sep 29 '22 12:09

Mojtaba Hosseini