Disable background taps while SwiftUI Menu is active?

Does anyone know if there is a way to disable background taps while a Menu is active? Currently the only way to dismiss a Menu is to tap away but if one taps something tappable, like a button or nav link the Menu is dismissed and the tap is registered.

This is isn't the biggest deal in the world, however I have noticed an issue when the button tap opens a sheet. If the sheet opening tap is pressed while the menu is open, the sheet will not open and the following error is printed in the console.

Attempt to present <TtGC7SwiftUI29PresentationHostingControllerVS_7AnyView: 0x7ff2ff22e000> on <TtGC7SwiftUI19UIHostingControllerV7Topline13MenuSheetTest: 0x7ff2fdf23b90> (from <TtGC7SwiftUI19UIHostingControllerV7Topline13MenuSheetTest: 0x7ff2fdf23b90>) which is already presenting <_UIContextMenuActionsOnlyViewController: 0x7ff2ff22d690>.

I have the following sample snippet that will demonstrate this behavior:

@State var sheetOpen = false
   
  var body: some View {
    HStack {
      Button {
        sheetOpen.toggle()
      } label: {
        Text("Open Sheet")
      }

      Spacer()

      Menu("Open menu") {
        Button {
          print("Option 1 pressed")
        } label: {
          Text("Option 1")
        }
        
        Button {
          print("Option 2 pressed")
        } label: {
          Text("Option 2")
        }
      }
    }
    .padding()
    .sheet(isPresented: $sheetOpen) {
      Text("Sheet is open")
    }
  }

Has anyone else encountered this behavior before? If I could disable taps while the Menu is open I could avoid this "already presenting" error entirely. I have also looked at setting up a proxy variable for when the Menu is open or not but there doesn't seem like a universal solution that would work in all cases.

Any help would be appreciated, thanks!

It's also worth noting that I have not been able to reproduce this issue on SwiftUI Previews, for what it's worth

I've also noticed that using DispatchQueue to introduce a tiny delay (0.01 seconds) when toggling the binding variable controlling sheet presentation (in the sample case sheetOpen) seems to prevent the dual presentation error

I have the same issue:

Here is my code:

Menu {

                Picker("Topic", selection: $selectedTopic) {

                    Text("Unassigned")

                        .tag(nil as Topic?)

                    Divider()

                    ForEach(topics, id:.id) { topic in

                        Text(topic.name!).tag(topic as Topic?)

                            .foregroundColor(topic.foregroundColor)

                    }

                }

            }

            

        label: {

            Image(systemName: "circle.grid.3x3.circle")

                .font(.system(size: 26,weight: .ultraLight))

                .foregroundColor(learningSet.topic?.foregroundColor ?? .primary)

                .frame(width:30, height: 30)

        }

This correctly shows the menu and if I select an item, the .onchange fires like it should.

Problem is if the user clicks on some other view on the screen while the is active, it fires that views .onTappedGesture.

I tried different combinations of highPriorityGesture and SimultaneousGesture to no avail.

I also tried the approach of adding a full screen view In between the view with the menu and the underlying views and added an onTap to it. That approach works fine with one issue: If the user click on the currency selected item, the .onChange for the picker doesn't fire, the menu disappears but its .onDisappear doesn't fire. So there is no way to know the menu is no longer visible, and hide my full screen view.

Any help would be greatly appreciated.

Thanks.

Disable background taps while SwiftUI Menu is active?
 
 
Q