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!

Post not yet marked as solved Up vote post of artclark Down vote post of artclark
106 views

Replies

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