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

SwiftUI Bug:Toolbar中添加Popover导致Mac应用内存激增运行卡顿

SwiftUI macOS Toolbar Popover 内存泄漏问题

我发现了一个SwiftUI Bug:在Mac应用的Toolbar中添加Popover后,应用整体性能会大幅下降,只要执行任意操作,内存占用就会持续攀升。
值得注意的是,触发该问题甚至不需要弹出Popover本身——只要代码中存在相关定义就足够了,移除Popover相关代码后应用运行状态会立即恢复正常。

复现代码

以下测试代码实现了一个简单的可拖拽视图,添加Popover相关代码后,拖拽该视图仅3~5秒就能观察到应用卡顿、内存上涨的问题:

import SwiftUI

struct ContentView: View {
    @State private var popUpPresented = false
    @State private var position: CGPoint = .zero
    @State private var offset: CGPoint = .zero
    
    var body: some View {
        ZStack {
            Rectangle()
                .overlay(Text("Drag me around").foregroundColor(.black))
                .position(position)
                .frame(width: 200, height: 150)
                .gesture(DragGesture(minimumDistance: 0, coordinateSpace: .local)
                    .onChanged { value in
                        position.x = offset.x + value.translation.width
                        position.y = offset.y + value.translation.height
                    }
                    .onEnded { _ in
                        offset = position
                    })
        }
        .frame(width: 600, height: 600)
        
        .toolbar {
            Button (action: {popUpPresented.toggle()}) {Text("Popup")}
            .popover(isPresented: $popUpPresented, arrowEdge: .top) {}
        }
    }
}

实测短短几秒内内存占用就从20Mb涨到了40Mb以上。

验证方法

尝试删除Toolbar内的以下两行代码,即可观察到内存不再异常上涨:

Button (action: {popUpPresented.toggle()}) {Text("Popover")}
.popover(isPresented: $popUpPresented, arrowEdge: .top) {Text("Any text")}

复现环境

  • Xcode 12
  • Xcode 13

恳请社区帮忙定位问题原因并提供可行的 workaround,非常感谢!

内容的提问来源于stack exchange,提问作者Jayāk

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.10.02 08:18:00