首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >SwiftUI - confirmationDialog在LazyVStack内部有异常行为。

SwiftUI - confirmationDialog在LazyVStack内部有异常行为。
EN

Stack Overflow用户
提问于 2022-11-09 15:10:17
回答 1查看 17关注 0票数 1

我有一个包含n个子视图的ScrollView和一个LazyVStack。每个子视图都有一个按钮,该按钮将显示一个确认对话框,确认对话框在子视图中创建。

由于某种原因,确认对话框在看到3个(或多或少)子视图后无法工作,您可以多次按下该按钮,但不会立即显示该对话框,如果在滚动时等待,突然每个对话框都会一个接一个弹出。

视频测试

测试代码:

代码语言:javascript
复制
struct ContentView: View {
    var body: some View {
        ScrollView {
            LazyVStack(spacing: 50) {
                ForEach(0...100, id: \.self) { _ in
                    SubView()
                }
            }
        }
        .padding()
    }
}


struct SubView: View {
    
    @State var flag = false
    
    var body: some View {
        ZStack(alignment: .bottom) {
            RoundedRectangle(cornerRadius: 30)
                .frame(height: 500)
                .foregroundColor(.gray)
                .overlay {
                    Button("Press me") {
                        flag.toggle()
                    }
                    .confirmationDialog("", isPresented: $flag, actions: {
                        Button(role: .none) {
                           print("option 1")
                        } label: {
                            Text("option 1")
                        }
 
                        Button(role: .cancel) {
                            flag = false
                        } label: {
                            Text("cancel")
                        }
                    })
                }
        }
    }
}
EN

回答 1

Stack Overflow用户

发布于 2022-11-09 15:45:09

方法

  • confirmationDialog移到LazyVStack之外

代码

代码语言:javascript
复制
struct ContentView: View {
    @State private var flag = false
    
    var body: some View {
        ScrollView {
            LazyVStack(spacing: 50) {
                ForEach(0...100, id: \.self) { _ in
                    SubView(flag: $flag)
                }
            }
            .confirmationDialog("", isPresented: $flag) {
                Button(role: .none) {
                    print("option 1")
                } label: {
                    Text("option 1")
                }
                
                Button(role: .cancel) {
                    flag = false
                } label: {
                    Text("cancel")
                }
            }
        }
        .padding()
    }
}

struct SubView: View {
    
    @Binding var flag: Bool
    
    var body: some View {
        ZStack(alignment: .bottom) {
            RoundedRectangle(cornerRadius: 30)
                .frame(height: 500)
                .foregroundColor(.gray)
                .overlay {
                    Button("Press me") {
                        flag.toggle()
                    }
                }
        }
    }
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/74377118

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档