onTapGesture in Menu or not working

Hi, after update to Swift6 and iOS 18 my code is not working anymore. This is code from documentation with onTapGesture (instead of primaryAction) and it's not working. OnTapGesture is not starting when user click.

            Menu {
                Button(action: {}) {
                    Label("Add to Reading List", systemImage: "eyeglasses")
                }
                Button(action: {}) {
                    Label("Add Bookmarks for All Tabs", systemImage: "book")
                }
                Button(action: {}) {
                    Label("Show All Bookmarks", systemImage: "books.vertical")
                }
            } label: {
                Label("Add Bookmark", systemImage: "book")
            } onTapGesture: {
                print(" Print on tap gesture")
            }

primaryAction isn't working either. I mean, primary action works but menu is not showing inside buttons.

            Menu {
                Button(action: {}) {
                    Label("Add to Reading List", systemImage: "eyeglasses")
                }
                Button(action: {}) {
                    Label("Add Bookmarks for All Tabs", systemImage: "book")
                }
                Button(action: {}) {
                    Label("Show All Bookmarks", systemImage: "books.vertical")
                }
            } label: {
                Label("Add Bookmark", systemImage: "book")
            } primaryAction: {
                print("")
            }

I also tried to overlay the menu, no effect.

ZStack {
                Menu {
                    Button(action: {}) {
                        Label("Add to Reading List", systemImage: "eyeglasses")
                    }
                    Button(action: {}) {
                        Label("Add Bookmarks for All Tabs", systemImage: "book")
                    }
                    Button(action: {}) {
                        Label("Show All Bookmarks", systemImage: "books.vertical")
                    }
                } label: {
                    Label("Add Bookmark", systemImage: "book")
                }
                Color.clear
                    .contentShape(Rectangle())
                    .onTapGesture {
                        print("Tap gesture recognized")
                    }
            }

Please help. How to start action when user is clicking on menu?

Answered by DTS Engineer in 805228022

You said:

primaryAction isn't working either. I mean, primary action works but menu is not showing inside buttons.

Could you please elaborate on this? did you long press on the menu to trigger the menu presentation ?

While using Menu(content🏷️ primaryAction:) API, the primary action gets performed when the user taps or clicks on the body of the control, and the menu presentation happens on a secondary gesture, such as on long press.

You said:

primaryAction isn't working either. I mean, primary action works but menu is not showing inside buttons.

Could you please elaborate on this? did you long press on the menu to trigger the menu presentation ?

While using Menu(content🏷️ primaryAction:) API, the primary action gets performed when the user taps or clicks on the body of the control, and the menu presentation happens on a secondary gesture, such as on long press.

Seeing the same issue here.

Sample code:

Menu {
    Button(action: {}, label: {
        Image(systemName: "book")
    })
} label: {
    Image(systemName: "book")
}
.onTapGesture {
    print("Tapped")
}

Expected output: "Tapped" gets printed when the menu is tapped.

Actual output: On iOS 18, "Tapped" does not get printed. on iOS 17 "Tapped" does get printed.

I find this to be a pretty major regression.

It looks like tvOS didn't get the x.x.1 release that iOS and iPadOS both received, so this is still very broken.

@orekpl were you able to find a workaround?

@DTS Engineer It seems that the tap event only fires if the Select button is double-clicked.

I've tried replacing the .onTapGesture with:

  • .onTapGesture(count: 1) { ... }
  • .gesture(TapGesture().onEnded { ... })
  • .highPriorityGesture(TapGesture().onEnded { ... })

These all produce the same results, where the closure only runs when the Select button is very quickly double-clicked.

I am not using these tap gesture listeners on a Menu, but just on a SwiftUI view, so this isn't isolated to the Menu view type.

In my experience, this is not reproducible on tvOS 18 Simulators, but is reproducible on physical hardware. As additional context, this happens while using both versions of the Siri remote (A1513 and A2540) and on both Apple TV models that I've tested with (A1625 and A2169).

As previously noted by @aanderson27 , this is a regression that has started happening with tvOS 18 and does not affect code running on tvOS 17.x or earlier.

Following up, I was able to stack .onLongPressGesture(minimumDuration: 0.01) { ... } and .onLongTouchGesture(minimumDuration: 0.01) {...} successfully as a workaround. Otherwise, I've been having some issues wirelessly debugging on a physical device and haven't tried using simultaneousGesture yet

onTapGesture in Menu or not working
 
 
Q