title: SwiftUI: NavigationView/List with programmatic selection under macOS
body: <p>I have a NavigationView/List combination that allows programmatic selection. The basic concept is similar to the one described here: <a href="https://www.hackingwithswift.com/quick-start/swiftui/how-to-use-programmatic-navigation-in-swiftui" rel="nofollow noreferrer">https://www.hackingwithswift.com/quick-start/swiftui/how-to-use-programmatic-navigation-in-swiftui</a></p>
  <p>With so many things, this works fine on iOS, but on macOS there's an issue: the EmptyView from the NavigationView becomes visible as soon as an item is selected:</p>
  <p><a href="https://i.stack.imgur.com/pzInu.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/pzInu.png" alt="enter image description here" /></a></p>
  <p>Does anybody know how to remove this unwanted EmptyView()?</p>
  <p>Here's a demo project:</p>
  <pre><code>struct ContentView: View {
      @ObservedObject private var state = State()
  
      var body: some View {
          NavigationView {
              VStack {
                  List(state.items, id: \.self, selection: $state.selected) { item in
                      Text(item)
                          .buttonStyle(.borderless)
                  }
  
                  // navigation to the detail view if an item is selected
                  if let selected = state.selected {
                      NavigationLink(
                          destination: Text(selected),
                          isActive: state.hasSelectionBinding
                      ) {
                          EmptyView()
                      }
                  }
              }
          }
      }
  }
  
  class State: ObservableObject {
      @Published var items: [String] = [&quot;Item 1&quot;, &quot;Item 2&quot;, &quot;Item 3&quot;]
      @Published var selected: String?
  
      var hasSelectionBinding: Binding&lt;Bool&gt; {
          Binding(
              get: { self.selected != nil },
              set: {
                  if $0 == false {
                      self.selected = nil
                  }
              }
          )
      }
  }
  </code></pre>
  <p>EDIT 1: I've tried putting the NavigationLink into a background modifier on the stack, but now the &quot;Empty View&quot; appears next to &quot;Item 3&quot;:</p>
  <pre><code>    var body: some View {
          NavigationView {
              VStack {
                  List(state.items, id: \.self, selection: $state.selected) { item in
                      Text(item)
                          .buttonStyle(.borderless)
                  }
                  
              }
              .background(Group {
                  // navigation to the detail view if an item is selected
                  if let selected = state.selected {
                      NavigationLink(
                          destination: Text(selected),
                          isActive: state.hasSelectionBinding
                      ) {
                          EmptyView()
                      }
                  }
                  
              })
          }
      }
  </code></pre>