You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

SwiftUI菜单打开时触发Sheet不显示及后续失效问题求助

SwiftUI菜单与Sheet冲突的真机Bug及解决方案

问题现象

当菜单处于打开状态时,点击背景中触发Sheet的齿轮按钮,菜单会自动关闭,但Sheet并未弹出。后续无论点击该按钮多少次都无反应,首次点击时日志会打印如下警告:

[Presentation] Attempt to present
<TtGC7SwiftUI29PresentationHostingControllerVS_7AnyView>:
0x105818200> on
<TtGC7SwiftUI19UIHostingControllerGVS_15ModifiedContentVS_7AnyViewVS_12RootModifier_>:
0x10601e000> (from
<TtGC7SwiftUI19UIHostingControllerGVS_15ModifiedContentVS_7AnyViewVS_12RootModifier_>:
0x10601e000>) which is already presenting
<_UIContextMenuActionsOnlyViewController: 0x107308e30>.

问题原因

SwiftUI底层将菜单和Sheet都以模态视图的方式呈现,而UIKit不允许同时展示两个模态视图,导致Sheet无法正常弹出,且后续按钮状态异常。

现有思路的困境

  • 禁用背景交互:希望在菜单打开时禁用菜单外所有内容的用户交互,但无法可靠检测菜单是否处于打开状态,暂未找到完善实现方式。
  • 先关菜单再开Sheet:不清楚如何主动关闭已打开的菜单,无法实现该逻辑。

复现代码

struct ContentView: View {
    
    @State private var displaySettings: Bool = false
    
    var body: some View {
        Button {
            displaySettings = true
        } label: {
            Image(systemName: "gearshape.fill")
                .resizable()
                .aspectRatio(contentMode: .fit)
                .frame(width: 50)
        }
        .toolbar {
            ToolbarItem(placement: .bottomBar) {
                Menu {
                    Button(action: {}) {
                        Label("Reset", systemImage: "arrow.counterclockwise")
                    }
                } label: {
                    Image(systemName: "ellipsis.circle")
                }
            }
        }
        .sheet(isPresented: $displaySettings) {
            Text("Settings")
        }
    }

}

⚠️ 注意:该问题仅在真机上出现,模拟器中可正常显示Sheet。


内容的提问来源于stack exchange,提问作者Mischa

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.15 09:27:46