-1

With the new navigation logic starting with iOS 16 how are you supposed to upgrade navigationBarItems to toolbar? My app has a number of views with a plus button on the upper right corner of the view that link to new views. Below are the old style with navigationBarItems and new style with toolbar. In the example below in MyNewView I'm not sure how to handle the navigation link. It gives a warning that the navigation link initializer is unused.

struct MyOldView: View {

    var body: some View {

        VStack {
            Text("The Old View")
        }
        .navigationTitle("Our Old View")
        .navigationBarItems(trailing: NavigationLink (destination: GoToNewView()) {
            Image(systemName: "plus")
                .resizable()
                .frame(width: 18, height: 18)
        })
    }
}

struct MyNewView: View {

    var body: some View {

        VStack {
            Text("The New View")
        }
        .navigationTitle("Our New View")
        .toolbar {
            ToolbarItem(placement: .navigationBarTrailing) {
                Button( action: {

                    NavigationLink("", destination: GoToNewView())

                }){
                    Image(systemName: "plus")
                        .resizable()
                        .frame(width: 18, height: 18)

                }
            }
        }
    }
}

struct GoToNewView: View {

    var body: some View {

        VStack {
            Text("Here is our new view")
        }
    }
}
Galen Smith
  • 299
  • 2
  • 14

1 Answers1

1

A NavigationLink is a View and can’t be passed as the action closure of the Button initializer. Since NavigationLink acts as a button you could just add a NavigationLink without the Button:


ToolbarItem(placement: .navigationBarTrailing) {
    NavigationLink(destination: GoToNewView()) {
        Image(systemName: "plus")
            .resizable()
            .frame(width: 18, height: 18)
    }
 }

whiteio
  • 161
  • 5