Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Tap Action not working when Color is clear SwiftUI

Tags:

ios

swift

swiftui

my tapAction is not recognizing a tap when my foregroundColor is clear. When i remove the color it works fine.

That's my code:

ZStack {
    RoundedRectangle(cornerRadius: 0)
        .foregroundColor(Color.clear)
        .frame(width: showMenu ? UIScreen.main.bounds.width : 0)
        .tapAction {
            self.showMenu.toggle()
        }
    
    RoundedRectangle(cornerRadius: 5)
        .foregroundColor(Color.green)
        .shadow(radius: 5, y: 2)
        .padding(.trailing, 50)
        .frame(width: showMenu ? UIScreen.main.bounds.width : 0)
}
.edgesIgnoringSafeArea(.top)
like image 895
SwiftiSwift Avatar asked Jun 29 '19 18:06

SwiftiSwift


1 Answers

The accurate way is to use .contentShape(Rectangle()) on the view. Described in this tutorial:

control-the-tappable-area-of-a-view by Paul Hudson @twostraws

VStack {
    Image("Some Image").resizable().frame(width: 50, height: 50)
    Spacer().frame(height: 50)
    Text("Some Text")
}
.contentShape(Rectangle())
.onTapGesture {
    print("Do Something")
}

how-to-control-the-tappable-area-of-a-view-using-contentshape stackoverflow

like image 54
51N4 Avatar answered Oct 18 '22 13:10

51N4