Does NavigationLink function correctly under macOS on anything other than List

Viewed 72

Has anyone got a working macOS example of a navigationLink within anything other than a List that works as expected ?

On some testing, I find that it works fine if it is with a List, however, in ScrollView, VStack or LazyHGrid it only updates the destination as expected on every second click.

Below is my test code. (ignore the swipe stuff - but thanks to here for the insights). If I use List as the container, the detail views appear as expected. Replacing List with either of the other 3 results in every second click producing a blank detail. I.E. click, first, second, third, fourth and it delivers first, blank, third, blank.

Is this just a macOS, "live with until Apple fixes it" or am I missing something fundamental ?

TIA

Alan

import SwiftUI

struct ContentView: View {
  enum SwipeHorizontalDirection: String {
      case left, right, none
  }
  @State private var swipeHorizontalDirection:SwipeHorizontalDirection = .none
  let rows = Array(repeating: GridItem(.fixed(100)), count: 6)
  let tiles = [
    TileView( title: "first"),
    TileView( title: "second"),
    TileView( title: "third"),
    TileView( title: "fourth")
  ]
  @State private var selectedTileID: UUID? = nil
  var body: some View {
    VStack {
      Text("A Test View")
      NavigationView {
//        VStack {
//        LazyHGrid(rows: rows) {
//        ScrollView {
        List {
        ForEach (0 ..< tiles.count) { index in
          NavigationLink(tiles[index].title, destination: tiles[index], tag: tiles[index].id, selection: $selectedTileID)
              }
        }
      }
    }
    .gesture(DragGesture()
              .onEnded{
      if $0.startLocation.x > $0.location.x {
          self.swipeHorizontalDirection = .left
      } else if $0.startLocation.x == $0.location.x {
          self.swipeHorizontalDirection = .none
      } else {
          self.swipeHorizontalDirection = .right
      }
      let nextID = nextItemAdjacentActive(direction: swipeHorizontalDirection)
      selectedTileID = nextID
    }
    )
  }

  func nextItemAdjacentActive(direction: SwipeHorizontalDirection) -> UUID
  {
    var foundIndex = 0
    for index in 0 ..< tiles.count {
      if tiles[index].id == selectedTileID {
        if direction == .right {
          if (index + 1) < tiles.count {
            foundIndex = index+1
          }
          else { foundIndex = 0}
        }
        else if direction == .left {
          if (index > 0) {
            foundIndex = index-1
          }
          else {
            foundIndex = tiles.count - 1
          }
        }
      }
    }
    return tiles[foundIndex].id
  }

}

struct ContentView_Previews: PreviewProvider {
  static var previews: some View {
    ContentView()
  }
}

struct TileView: View, Identifiable {
  var id = UUID()
  var title:String
  
  var body: some View {
    VStack {
      Text(title).font(.largeTitle)
    }
  }
  
}


0 Answers
Related