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

火山引擎 最新活动