How can I slide the Slider under a View in SwiftUI?

There are two views (Slider and Button) under a view (Rectangle):

struct ContentView: View {
    //@State var val = false
    @State var floatVal = 0.0
    var body: some View {
        ZStack {
            VStack {
                Text("Control penetration")
                //Slider can't slide
                Slider(value: $floatVal, in: 0...100)
                //Toggle can't toggle too
                //Toggle(isOn: $val){EmptyView()}
                //only button can tap
                Button(action: {
                    Text("Can you tap me???")

I use allowsHitTesting() to make the click penetrate to the bottom.

But only the Button can respond to click, the Slider can not slide!

What’s wrong with it? How can I make the Slider respond slide too? thanks a lot!

hi Hopy! I guess the main reason is because you don’t tap a slider. A SO answer says you can’t scroll a scrollview through a top layer either.

Until SwiftUI gets its own version of userInteractionEnabled, you might try creating a UIViewRepresentable version of the rectangle


其实我是想在Slider上覆盖一个有圆形空洞(用Path eofFill组合1个矩形和1个圆形)的View,然后只有空洞中的Slider可以操作,其它部分的控件不可以响应用户的操作。



@hopy Please post your comments in English in order to get answers. Thank you!