【问题标题】:SwiftUI Button not being tapped未点击 SwiftUI 按钮
【发布时间】:2020-07-02 16:18:55
【问题描述】:

我在 swiftui 中的 geometryReader 内的 Vstack 内的 ZStack 内的 Vstack 内的 Hstack 内嵌入了一个按钮,该按钮不会被点击。我在里面放了一个打印语句,每当我尝试点击按钮时,打印语句都不会打印。有谁可以帮我离开这里吗?谢谢。这是我的代码:

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)
    }
}

【问题讨论】:

    标签: ios swift swiftui


    【解决方案1】:

    刚刚在 XCode 11.5 和 12.0 上测试了相同的代码,并且按钮工作正常...确保在模拟器上测试它而不是在预览/画布上

    【讨论】:

      猜你喜欢
      • 2021-03-21
      • 2019-12-11
      • 2022-01-03
      • 2020-06-07
      • 1970-01-01
      • 2020-09-06
      • 2021-06-04
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多