SwiftUI疑难杂症(1):sheet content多次执行

在开发SwiftUIDev的时候,今天突然发现了一个弹出sheet时候content多次执行,手动dismiss也会多次执行的异常情况,能复现问题的代码如下:

Swift 复制代码
struct Test: View {
    @State private var present = false

    var body: some View {
        return VStack {
            Button("open") {
                present = true
            }
        }
        .toolbar {
            ToolbarItem {
//                Button("open") {
//                    present = true
//                }
                Menu {
                    Button("sample", action: {
                        present = true
                    })
                } label: {
                    Image(systemName: "curlybraces")
                }
            }
        }
        .sheet(isPresented: $present) {
            print("=====show sheet")
            return Text("sheet")
        }
        .navigationTitle("test")
    }
}

经测试,发现该问题仅在.toolbar { ToolbarItem { Menu {}}}.sheet结构的时候才会发生。如果把Menu换成Button,或者把.sheet移到.toolbar之前都不会发生这个问题。环境iOS16+Xcode14。

相关推荐
RickeyBoy2 小时前
SwiftUI 如何实现 Infinite Scroll?
ios·面试
壹方秘境5 小时前
厌倦了那些看着像一个模版复刻出来的抓包工具,我开发了一款iOS端HTTPS抓包调试工具
ios·程序员·产品
开心就好20259 小时前
不同阶段的 iOS 应用混淆工具怎么组合使用,源码混淆、IPA混淆
后端·ios
世界不及妳微笑9 小时前
关于Xcode26.4 踩坑适配
ios·xcode
iceiceiceice9 小时前
iOS 26 适配 | 使用 `hidesSharedBackground` 保持导航栏按钮原有样式
ios·objective-c·编程语言
SY.ZHOU12 小时前
移动端架构体系(一):组件化
android·ios·架构·系统架构
人月神话Lee18 小时前
一个iOS开发者对Flutter中Widget、Element和RenderObject的理解
前端·flutter·ios
2501_9160074718 小时前
iOS 开发工具有哪些 按开发流程整理的工具清单
ide·vscode·ios·objective-c·个人开发·swift·敏捷流程
2501_9159214318 小时前
2026 iOS 上架新趋势 iOS 发布流程模块化
android·ios·小程序·https·uni-app·iphone·webview