-1

Currently, I have a settings button inside the toolbar that when I tap on it it will open the SettingsView(). Here's my implementation below.

@State private var goToSettings = false

NavigationView {
   ZStack {
      NavigationLink(destination: SettingsView(), isActive: $goToSettings) {
      }
   }
   .toolbar {
      Button(role: .destructive, action: {
         goToSettings = true
      }) {
         Label("Settings", systemImage: "gearshape.fill").foregroundColor(colorForeground)
      }
   }
}

Now what confuses me right now is how to implementation this with NavigationStack? Where should I put NavigationLink because it seems to not work inside the .toolbar?

Thanks!

Lawrence Gimenez
  • 2,662
  • 4
  • 34
  • 52

1 Answers1

1

The solution was a little straightforward I found out.

Instead of NavigationView, you use the new and shiny NavigationStack. And use .navigationDestination() instead of NavigationLink.

@State private var goToSettings = false

NavigationStack {
   ZStack {
      // Some more codes
   }
   .toolbar {
      Button(role: .destructive, action: {
         goToSettings = true
      }) {
         Label("Settings", systemImage: "gearshape.fill").foregroundColor(colorForeground)
      }
   }
   .navigationDestination(isPresented: $goToSettings, destination: {
       SettingsView()
    })
}
Lawrence Gimenez
  • 2,662
  • 4
  • 34
  • 52