4

I have a NavigationLink with an attached .contextMenu modifier. Note that the NavigationLink is inside of a ForEach which is inside of a LazyHStack which is inside of a ScrollView. (I'm creating a horizontal scrolling row of "cards", much like many Apple apps.)

Now, both the navigation and the context menu work; if I single-tap, it navigates to the view I specify and if I long-press it presents a context menu. The issue is that the context menu is focused on a highlighted grey view due to the navigation link highlighting.

Screenshot of issue

I considered removing the highlighting of the navigation link via a view modifier to no avail. The grey highlight still applies to the context menu view.

.onAppear {
    UITableViewCell.appearance().selectionStyle = .none
    UITableView.appearance().allowsSelection = false
}

So, how can I have a view that is both navigatable on tap as well as presents a context menu focused on the view when a long-press gesture is triggered? Note that I am not trying to have a NavigationLink inside a ContextMenu (as many questions seem to be asking); I am trying to have a ContextMenu attached to a NavigationLink.

user1234
  • 689
  • 1
  • 8
  • 22

1 Answers1

2

By provided description I assume the solution will be to activate NavigationLink programmatically on tap, like (pseudo-code):

Card()
  .onTapGesture {
     activeCard = item
  }
  .contextMenu {
     // ...
  }
  .background(
     NavigationLink(destination: SomeView(), tag: item, selection: $activeCard) {
       EmptyView()
     }
  )

See next for complete example https://stackoverflow.com/a/58898046/12299030.

Asperi
  • 228,894
  • 20
  • 464
  • 690
  • Hello, I was aware of a similar solution, though it feels quite a bit hacky to me. Moments ago I actually just found a solution [here](https://stackoverflow.com/a/62311089/2060140) that accomplishes what I want (by applying a custom styling--`PlainButtonStyle` does not seem to work). Edit: I see the other solution (preferable to me) was also provided by you. Kudos to you. – user1234 Dec 19 '20 at 17:17
  • )) that is possible alternate... without provided code it is hard to guess. – Asperi Dec 19 '20 at 17:20