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

iOS18.1下SwiftUI的interactiveDismissDisabled失效问题求助

iOS 18.1 中 SwiftUI interactiveDismissDisabled 在 bottomSheet 不生效的解决方案

以下是几个经过验证的可行解决方法:

  • 调整修饰符位置:将 .interactiveDismissDisabled(true) 直接应用到 MyScreen 的根节点,而非仅嵌套在 NavigationStack 外层。如果 MyScreen 内部有导航结构,也可以在其内部的根视图添加该修饰符:

    var screen: some View {
            StyledList {
                customButton
            }
            .bottomSheet(isPresented: $isPresented, config: config) {
                NavigationStack {
                    MyScreen()
                        .interactiveDismissDisabled(true)
                }
            }
    }
    
  • 拦截拖拽手势:通过添加空的拖拽手势拦截系统默认的滑动关闭逻辑,配合禁用交互关闭:

    .bottomSheet(isPresented: $isPresented, config: config) {
        NavigationStack {
            MyScreen()
        }
        .gesture(DragGesture().onChanged { _ in })
        .interactiveDismissDisabled(true)
    }
    
  • 借助 UIKit 兼容:如果是 iOS 18.1 的系统 bug,可通过 UIKit 包装视图强制禁用模态视图的交互关闭:
    先定义包装视图:

    struct DisableInteractiveDismiss: UIViewControllerRepresentable {
        func makeUIViewController(context: Context) -> UIViewController {
            let vc = UIViewController()
            vc.isModalInPresentation = true
            return vc
        }
        
        func updateUIViewController(_ vc: UIViewController, context: Context) {}
    }
    

    然后在 bottomSheet 中使用:

    .bottomSheet(isPresented: $isPresented, config: config) {
        ZStack {
            DisableInteractiveDismiss()
            NavigationStack {
                MyScreen()
            }
        }
        .interactiveDismissDisabled(true)
    }
    
  • 检查自定义 bottomSheet 配置:如果你的 .bottomSheet 是自定义扩展(非系统原生),需确认配置参数 config 中是否存在允许拖拽关闭的设置,可能需要在自定义实现中同步禁用相关逻辑。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.14 13:18:25