0

I have a Scrollview (pretty basic)

        ScrollView(.vertical, showsIndicators: false, content: {
            VStack(content: {
                ForEach(self.elements, id: \.self) { _ in
                    Rectangle()
                        .frame(height: 30)
                        .background(Color.green)
                }
            })
        })
        .bottomInnerShadow(color: Color.black.opacity(0.45), radius: 0.5)

And I am using a custom Specifier define as followed

fileprivate extension View {
    func bottomInnerShadow(color: Color, radius: CGFloat = 0.1) -> some View {
        modifier(BottomInnerShadow(color: color, radius: min(max(0, radius), 1)))
    }
}

private struct BottomInnerShadow: ViewModifier {
    var color: Color = .gray
    var radius: CGFloat = 0.1

    private var colors: [Color] {
        [color.opacity(0.49), color.opacity(0.0), .clear]
    }

    func body(content: Content) -> some View {
        GeometryReader { geo in
            content
                .overlay(LinearGradient(gradient: Gradient(colors: self.colors), startPoint: .bottom, endPoint: .top)
                            .frame(height: self.radius * self.minSide(geo)),
                         alignment: .bottom)
        }
    }

    func minSide(_ geo: GeometryProxy) -> CGFloat {
        CGFloat(3) * min(geo.size.width, geo.size.height) / 2
    }
}

It allows me to create a shadow. The issue here is that it is blocking my scrollview actions. I can't scroll anymore where the shadow appear. I tried allowsHitTesting(false) && .disabled(true) but it doesn't work.

Do you guys have an idea how I could overpass that?

Thank you in advance for any future help :D

PS: I already checked that link -> How to disable user interaction on SwiftUI view? and also a link on the apple forum (don't have it here).

1 Answer 1

0

Just change ViewModifier to

private struct BottomInnerShadow: ViewModifier {
    var color: Color = .gray
    var radius: CGFloat = 0.1

    private var colors: [Color] {
        [color.opacity(0.49), color.opacity(0.0), .clear]
    }

    func body(content: Content) -> some View {
        GeometryReader { geo in
            ZStack {
                Rectangle().fill(Color.clear).overlay(LinearGradient(gradient: Gradient(colors: self.colors), startPoint: .bottom, endPoint: .top)
                                        .frame(height: self.radius * self.minSide(geo)),
                                    alignment: .bottom)
                content
            }

        }
    }

    func minSide(_ geo: GeometryProxy) -> CGFloat {
        CGFloat(3) * min(geo.size.width, geo.size.height) / 2
    }
}
Sign up to request clarification or add additional context in comments.

1 Comment

This is nice, but I need the shadow on top so it overlays the scrollview. This is under

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.