Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SwiftUI overlay cancels touches

Tags:

swiftui

I have a button and I'd like to put a semi-transparent gradient overlay on top of it.

Button(action: {
  print("Pressed")
}) {
  Text("Press me")
}
.overlay(
  LinearGradient(
    gradient: Gradient(colors: [.clear, Color.black.opacity(0.3)]),
    startPoint: .top,
    endPoint: .bottom
  ).disabled(true)
)

Even though the gradient has disabled(true) it still eats the touches and doesn't forward them to the actual button. .allowsHitTesting(false) provides the same result.

Any idea what can be wrong?

Note: I know I can put the overlay just to Text("Press me") but I don't want it. (This is just example code showcasing the problem)


Edit: This issue is solved in Xcode 11.2 ✅

like image 786
VojtaStavik Avatar asked Oct 04 '19 11:10

VojtaStavik


1 Answers

The following code works on Xcode 11.2 / iOS 13.2

struct TestButtonWithOverlay: View {
    var body: some View {
        Button(action: {
            print("Pressed")
        }) {
            Text("Press me")
                .padding()
        }

        .overlay(
            LinearGradient(
                gradient: Gradient(colors: [.clear, Color.black.opacity(0.3)]),
                startPoint: .top,
                endPoint: .bottom
            )
            .allowsHitTesting(false) // !!! must be exactly here
        )
    }
}

struct TestButtonWithOverlay_Previews: PreviewProvider {
    static var previews: some View {
        TestButtonWithOverlay()
    }
}
like image 143
Asperi Avatar answered Sep 21 '22 16:09

Asperi