“操作”按钮在SwiftUI中无法正常运行

时间:2020-10-20 21:37:13

标签: ios swift swiftui

我正在创建带有操作的图像按钮,但是它们不会进入自己的相应视图。任何人都可以指出我在做什么错下面的代码中我错过了什么。感谢您的帮助。这是我的下面的代码。我用ZStack包装它,仍然没有任何工作。我看不见的东西是什么?

    struct DashboardView: View {
    
    @State  var showingNotifications = false
    @State  var showingMore = false
    @State  var showingARCam = false
    @State  var showingMapView = false
    @State  var showingProfile = false
    
    var body: some View {
     
        ZStack {
         
             Button(action: {
                withAnimation {
                    self.showingNotifications.toggle()
                }
            }) {
                Image("NotificationButton").offset(x: -120, y: -180)
            }.sheet(isPresented: $showingNotifications) {
                
                NotificationView()
                .edgesIgnoringSafeArea(.all)
            }
            
            Spacer()
            Button(action: {
                withAnimation {
                    self.showingMore.toggle()
                }
            }) {
                Image("NotificationButton").offset(x: 80, y: -180)
            }.sheet(isPresented: $showingMore) {
                
                 MoreView()
                .edgesIgnoringSafeArea(.all)
            }
            Spacer()
            Button(action: {
                withAnimation {
                    self.showingARCam.toggle()
                }
            }) {
                Image("AR").offset(x: 10, y: 65)
            }.sheet(isPresented: $showingARCam) {
                
                AR()
                
                .edgesIgnoringSafeArea(.all)
            }
            Spacer()
             Button(action: {
                withAnimation {
                    self.showingMapView.toggle()
                }
            }) {
                Image("MapIcon").offset(x: -120, y: 250)
            }.sheet(isPresented: $showingMapView) {
                
                MapView()
                .edgesIgnoringSafeArea(.all)
            }
                
 
            
            Button(action: {
                withAnimation {
                    self.showingProfile.toggle()
                }
            }) {
                Image("ProfileIcon").offset(x: 90, y: 250)
            }.sheet(isPresented: $showingProfile) {
                
                ProfileView()
                 .edgesIgnoringSafeArea(.all)
            }
        
         }
        
 
 
        .background(
            Image("Dashboard")
                .resizable()
                .edgesIgnoringSafeArea(.all)
                .frame(width: UIScreen.main.bounds.width, height: UIScreen.main.bounds.height))
               
      
    }
    
} 

1 个答案:

答案 0 :(得分:0)

删除offsets ... .offset修饰符不会更改视图的位置(并且不会影响布局),而仅更改绘图位置,因此您可以在一处看到图像,但是真正的按钮是位于不同的位置,因此点击图像无效。

 Button(action: {
    withAnimation {
        self.showingNotifications.toggle()
    }
}) {
    Image("NotificationButton") // .offset(x: -120, y: -180) // << here !!
}.sheet(isPresented: $showingNotifications) {