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




