1

我正在使用向下代码进行自定义警报,代码在没有 ScrollView 的情况下工作正常,只要您在代码中使用 ScrollView,代码就会混淆和损坏。scrollView 的问题是这样的,它会破坏代码,没有它就可以正常工作!我怎么能解决这个奇怪的问题?

代码的方式是这样的,点击信息按钮,所有屏幕都变得模糊和禁用,并且会出现自定义警报。它可以按预期工作而无需 ScrollView!

    struct ContentView: View {

        @State var showCustomAlertView: Bool = Bool()

        var body: some View {
    
            ZStack {
                VStack {             
                    HStack {
                        
                        Button(action: {
                            
                            withAnimation(.easeInOut(duration: 0.35)) { showCustomAlertView.toggle() }
    
                            print("info")
                            
                        }) { Image(systemName: "info.circle") }
                        
                        Spacer() 
                    }.padding()

                    Spacer()
                    
                    if showCustomAlertView { CustomAlertView(showCustomAlertView: $showCustomAlertView); Spacer() }
                    
                }
    
                VStack {
                    
                    
                    VStack { Button("toggle Button1") {
                        
                    print("toggle Button1")
                        
                    }.foregroundColor(Color.white).padding(.horizontal,25).padding(.vertical,5).background(Color.blue).cornerRadius(10) }
                    
       
                    //ScrollView (showsIndicators: true) {
    
                        
                        VStack(spacing: 10) {
                            
                            Text("Some text here ..." )
                            Text("Some text here ..." )
                            Text("Some text here ..." )
                            Text("Some text here ..." )
                        }
                        
                        
                    //}
        
     
                    VStack { Button("toggle Button2") {
                        
                        print("toggle Button2")
                        
                    }.foregroundColor(Color.white).padding(.horizontal,25).padding(.vertical,5).background(Color.blue).cornerRadius(10) }
                    
                    
                }.blur(radius: showCustomAlertView ? 3 : 0).disabled(showCustomAlertView)
       
            }
            
            
        }
    }

    struct CustomAlertView: View {
        
        @Binding var showCustomAlertView: Bool
        
        var body: some View {
    
            VStack { Text("CustomAlertView").bold().padding(); Button("dismiss") { print("dismiss");showCustomAlertView.toggle() } }
        
        }
    }
4

1 回答 1

1

使视图具有高于所有其他的警报,例如(使用 Xcode 12.1 / iOS 14.1 测试)

        ZStack {
            VStack {
                HStack {
                    
                    Button(action: {
                        
                        withAnimation(.easeInOut(duration: 0.35)) { showCustomAlertView.toggle() }

                        print("info")
                        
                    }) { Image(systemName: "info.circle") }
                    
                    Spacer()
                }.padding()

                Spacer()
                
                if showCustomAlertView { CustomAlertView(showCustomAlertView: $showCustomAlertView); Spacer() }
                
            }.zIndex(1)     // << here !!
于 2020-11-27T11:20:27.687 回答