简体   繁体   English

SwiftUI 按钮未被点击

[英]SwiftUI Button not being tapped

I Have a button embedded inside a Hstack inside a Vstack inside a ZStack inside a Vstack inside a geometryReader in swiftui that does not get tapped.我在 swiftui 中的 geometryReader 内的 Vstack 内的 Vstack 内的 Vstack 内嵌入了一个按钮,该按钮未被点击。 I put a print statement inside and whenever I tried to Tap the button, the print statement won't print.我在里面放了一个打印语句,每当我尝试点击按钮时,打印语句都不会打印。 Can anyone help me out here?有谁可以帮我离开这里吗? Thanks.谢谢。 Heres my code:这是我的代码:

struct DetailedGroupView: View {
    @State var actrualImage: Image?
    @State var title = "title"
    @State var description = "description"
    @State var sgName = "sgName"
    @State var membersCount = 0
    @Environment(\.presentationMode) var presentationMode
    var body: some View {
        GeometryReader{ geo in
            VStack{
                ZStack{
                    
                    (self.actrualImage ?? Image("earthPlaceholder"))
                        .resizable()
                        .aspectRatio(contentMode: .fill)
                    VStack{
                        
                        
                        Spacer()
                        HStack{

                            //This button doesn't work
                            Button(action: {
                                print("Button Tapped")   

                                self.presentationMode.wrappedValue.dismiss()
                            }, label: {
                                Image(systemName: "chevron.left").foregroundColor(.white)
                            }).padding()
                            
                            
                            Text(self.title)
                                .font(.largeTitle)
                                .fontWeight(.bold)
                                .multilineTextAlignment(.leading)
                                .foregroundColor(.white)
                                .padding()
                                .minimumScaleFactor(0.5)
                            Spacer()
                        }
                        HStack{
                            Text(self.description)
                                .font(.custom("Open Sans", size: 18))
                                .fontWeight(.ultraLight)
                                .multilineTextAlignment(.leading)
                                .foregroundColor(.white)
                                .padding()
                            Spacer()
                        }
                        Spacer()
                        HStack{
                            
                            
                            Image(systemName: "person.2.fill").foregroundColor(.white).padding(.leading)
                            Text("\(self.membersCount)")
                                .font(.custom("Open Sans", size: 12))
                                .fontWeight(.semibold)
                                .foregroundColor(.white)
                            Spacer()
                            Text(self.sgName)
                                .font(.custom("Open Sans", size: 12))
                                .fontWeight(.semibold)
                                .foregroundColor(.white)
                                .padding()
                        }.padding()
                        
                    }.frame(width: geo.size.width, height: 294)
                }.frame(width: geo.size.width, height: 294)
                    .clipShape(RoundedRectangle(cornerRadius: 12))
                    .edgesIgnoringSafeArea(.top)
                Spacer()
                ScrollView(showsIndicators: false){
                    VStack{
                        
                        
                        Spacer()
                        
                    }.onAppear{
                        self.actrualImage = Image("globePlaceholder")

                    }
                }
            }
            
        }.navigationBarBackButtonHidden(true)
    }
}

Just tested this same code on both XCode 11.5 and 12.0, and the button works fine... be sure to test it on a simulator not on the preview/canvas刚刚在 XCode 11.5 和 12.0 上测试了相同的代码,并且按钮工作正常...确保在模拟器上而不是在预览/画布上进行测试

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM