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

iOS17嵌套Sheet同时用presentationBackground引发内存泄漏崩溃

iOS 17嵌套Sheet内存崩溃问题分析

问题描述

我的应用里有两个嵌套的Sheet,原本运行正常。但给其中一个Sheet设置背景后,应用突然开始占用大量内存,几秒内就崩溃了。这个问题只出现在iOS 17.0到17.5版本中,触发条件是嵌套Sheet同时使用presentationBackground和presentationBackgroundInteraction;移除其中任意一个属性,应用就能正常运行。另外同时使用presentationDetents时也会出现同样的崩溃问题。想请教这是实现方式有问题,还是SwiftUI的bug?

代码示例

崩溃场景代码

// 几秒后崩溃
struct CrashingView: View {
    var body: some View {
        Text("-")
            .sheet(isPresented: .constant(true)) {
                Text("-")
                    .presentationBackground(.thickMaterial)
                    .presentationBackgroundInteraction(.enabled(upThrough: .large))
                    .sheet(isPresented: .constant(false)) {
                        Text("-")
                    }
            }
    }
}

正常运行代码

// 运行正常
struct NonCrashingView: View {
    var body: some View {
        Text("-")
            .sheet(isPresented: .constant(true)) {
                Text("-")
                    .presentationBackground(.thickMaterial)
                    // .presentationBackgroundInteraction(.enabled(upThrough: .large))
                    .sheet(isPresented: .constant(false)) {
                        Text("-")
                    }
            }
    }
}

问题结论与解决方案

这是SwiftUI在iOS 17.0-17.5版本中的已知bug,和你的实现方式无关。

  • 触发逻辑:嵌套Sheet同时组合使用presentationBackground+presentationBackgroundInteraction,或presentationBackground+presentationDetents时,SwiftUI内部存在内存管理漏洞,会导致内存持续泄漏直至崩溃。
  • 临时修复方案:
    • 避免在嵌套Sheet中同时使用上述组合修饰符;
    • 若需保留交互效果,可替换为自定义Sheet组件,或把内层Sheet改成非嵌套的全屏覆盖、弹窗等形式;
    • 升级至iOS 17.6及以上版本,苹果已在后续系统更新中修复该问题。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.22 21:50:56