Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SwiftUI - How to show shadow only on top side?

Tags:

ios

swift

swiftui

I am trying to give shadow to my VStack (only at top) but when I do like below shadow is visible to all sides like button, Text. But I am trying to give border to only container.

.background(Color.white // any non-transparent background
               .shadow(color: Color.red, radius: 10, x: 0, y: 0)
             )

I want UI like below enter image description here

Thank you for help

like image 420
Rahul Avatar asked Sep 12 '25 15:09

Rahul


1 Answers

Try using the mask(_:) modifier, as shown in this answer.

.background(
    Color.white // any non-transparent background
        .shadow(color: Color.red, radius: 10, x: 0, y: 0)
        .mask(Rectangle().padding(.top, -20)) /// here!
)

Result:

Red shadow only on top

like image 88
aheze Avatar answered Sep 15 '25 03:09

aheze