HomeiOS DevelopmentApply shadow solely exterior of a SwifUI clear button?

Apply shadow solely exterior of a SwifUI clear button?


I’ve a SwiftUI button with a clear background, a rounded border, and a shadow.

The difficulty is that as a result of the button is clear, the shadow is seen each inside and outside the rounded rectangle. I would love the shadow to look solely exterior the button, with out bleeding into the clear inside.

Apply shadow solely exterior of a SwifUI clear button?

Here’s a minimal instance:

import SwiftUI

#Preview("StackOverflow Clear Button Shadow") {
    let cornerRadius: CGFloat = 15

    Button {} label: {
        Textual content("Clear button")
            .font(.system(measurement: 14, weight: .semibold))
            .foregroundStyle(.black)
            .padding(.horizontal, 16)
            .padding(.vertical, 8)
            .body(peak: 40, alignment: .heart)
    }
    .background(.white)
    .clipShape(RoundedRectangle(cornerRadius: cornerRadius))
    .overlay {
        RoundedRectangle(cornerRadius: cornerRadius)
            .stroke(.pink, lineWidth: 1)
            .shadow(
                colour: .blue,
                radius: 2,
                x: 0,
                y: 0
            )
    }
    .padding(48)
    .background(Shade.white)
}

This produces a glow across the border, however the shadow can also be rendered contained in the clear space of the button.

What I need is:

  • hold the button background clear
  • hold the rounded border seen
  • apply the shadow/glow solely exterior the rounded rectangle
  • keep away from any shadow showing contained in the button

Is there a really useful SwiftUI method to masks or clip the shadow in order that it solely renders exterior the form?

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

- Advertisment -
Google search engine

Most Popular

Recent Comments