Home > Mobile >  onTapgesture disabling buttons of View SwiftUI
onTapgesture disabling buttons of View SwiftUI

Time:12-27

I have a view with two buttons, and I added .onTapGesture to toggle the view. But after I add .onTapGesture, buttons are underneath it and not responding. Is there any way to use both together

MenuView()

.onTapGesture{
self.isShowing.toggle()
}

CodePudding user response:

What you're looking for is a simultaneous gesture:

import SwiftUI

struct TestView: View {
  
  var body: some View {
    
    let tapGesture = TapGesture()
      .onEnded { _ in
        print("Menu tapped")
      }
    
    menuView
      .simultaneousGesture(tapGesture)
  }
  
  var menuView: some View {
    VStack {
      Button {
        print("Item 1 hit")
      } label: {
        Text("Item 1")
      }
      
      Button {
        print("Item 2 hit")
      } label: {
        Text("Item 2")
      }
    }
  }
}

struct TestView_Previews: PreviewProvider {
  
  static var previews: some View {
    Group {
      TestView()
    }
  }
}

  • Related