I have a SwiftUI button with a transparent background, a rounded border, and a shadow.
The issue is that because the button is transparent, the shadow is visible both outside and inside the rounded rectangle. I would like the shadow to appear only outside the button, without bleeding into the transparent interior.

Here is a minimal example:
import SwiftUI
#Preview("StackOverflow Transparent Button Shadow") {
let cornerRadius: CGFloat = 15
Button {} label: {
Text("Transparent button")
.font(.system(size: 14, weight: .semibold))
.foregroundStyle(.black)
.padding(.horizontal, 16)
.padding(.vertical, 8)
.frame(height: 40, alignment: .center)
}
.background(.white)
.clipShape(RoundedRectangle(cornerRadius: cornerRadius))
.overlay {
RoundedRectangle(cornerRadius: cornerRadius)
.stroke(.red, lineWidth: 1)
.shadow(
color: .blue,
radius: 2,
x: 0,
y: 0
)
}
.padding(48)
.background(Color.white)
}
This produces a glow around the border, but the shadow is also rendered inside the transparent area of the button.
What I want is:
- keep the button background transparent
- keep the rounded border visible
- apply the shadow/glow only outside the rounded rectangle
- avoid any shadow appearing inside the button
Is there a recommended SwiftUI way to mask or clip the shadow so that it only renders outside the shape?