2

I am creating image button with actions, but however they are not going to their own corresponding view. Can anyone point out of what am I doing wrong or what am I missing in my code below. thank you for the help. here is my code below. I am wrapping it with ZStack, and still no work. what is it missing that I can't see?

    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))
               
      
    }
    
} 
2
  • can you add more details on the issue you are facing? Commented Oct 21, 2020 at 1:17
  • you are using Images which we do not access to them, please change your image to systemImage, it would deferent result with deferent Image, because of Image size Commented Oct 21, 2020 at 2:05

1 Answer 1

1

Remove offsets... .offset modifier does not change location of view (and does not affect layout), but only place of drawing, so you see image in one place, but real button is located in different, so tapping image has no effect.

 Button(action: {
    withAnimation {
        self.showingNotifications.toggle()
    }
}) {
    Image("NotificationButton") // .offset(x: -120, y: -180) // << here !!
}.sheet(isPresented: $showingNotifications) {
Sign up to request clarification or add additional context in comments.

3 Comments

I am using .offset because I want to move the direction of the image. Unless you know a better way for it?
Manual layout is not recommended, because it might be broken on different device sizes. But if you like you can use .padding or .position.
so, when I add .position it works ok. However, the buttons dont work for each and every buttons I have.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.