I’m creating a SwiftUI view that has each Menu
parts and Button
s that set off the presentation of a sheet
.
I’ve observed an odd impact the place if the button-tap that often presents the sheet happens whereas a Menu
is presently offered, the sheet doesn’t current. Furthermore, subsequent faucets don’t current the sheet both. When this occurs, I see the next logged message:
Try to current <_TtGC7SwiftUI29PresentationHostingControllerVS_7AnyView_: 0x113811a00> on <_TtGC7SwiftUI19UIHostingControllerGVS_15ModifiedContentVS_7AnyViewVS_12RootModifier__: 0x112825600> (from <_TtGC7SwiftUI19UIHostingControllerGVS_15ModifiedContentVS_7AnyViewVS_12RootModifier__: 0x112825600>) which is already presenting <_UIContextMenuActionsOnlyViewController: 0x10c1183c0>.
This solely occurs if the faucet on the modal presenting button is fast – for longer presses, it really works as anticipated.
An entire app that displays this behaviour is as follows:
import SwiftUI
@primary
struct menuAndButtonApp: App {
var physique: some Scene {
WindowGroup {
ContentView()
}
}
}
struct ContentView: View {
@State var modalIsPresented = false
var physique: some View {
HStack(spacing: 28) {
Menu("Open Menu") {
Textual content("Menu Merchandise")
}
Button("Current Modal") {
modalIsPresented = true
}
}.sheet(isPresented: $modalIsPresented) {
Textual content("Here's a sheet")
}
}
}
This seems very very similar to a SwiftUI bug, however I’m attempting to find an alternate method or a workaround that may enable me to forestall this annoying behaviour. (One workaround I’ve confirmed is wrapping the button’s motion in DispatchQueue.primary.asyncAfter(.now() + 0.2) { }
to permit time for the context menu to be dismissed, however I would have to try this unconditionally as a result of I can not detect whether or not the context menu is offered at any given second.
This has been testing solely on Xcode 15, and displays on all iOS variations I’ve examined (15, 16 and 17).