Skip to main content

How to draw a shadow around a view

About 3 minSwiftSwiftUIArticle(s)bloghackingwithswift.comcrashcourseswiftswiftuixcodeappstore

How to draw a shadow around a view 관련

SwiftUI by Example

Back to Home

How to draw a shadow around a view | SwiftUI by Example

How to draw a shadow around a view

Updated for Xcode 15

Updated in iOS 16

SwiftUI gives us two ways of creating shadows: we can attach shadows directly to shape styles, but there’s also a dedicated shadow() modifier to draw shadows around views.

The former option is useful when you’re working with solid shapes, because you can create drop shadows easily:

Circle()
    .fill(.red.shadow(.drop(color: .black, radius: 10)))
    .padding()

Download this as an Xcode projectopen in new window

And you can create inner shadows for those shapes just as easily:

Circle()
    .fill(.red.shadow(.inner(color: .black, radius: 10)))
    .padding()

Download this as an Xcode projectopen in new window

You can control the color, radius, and position of the shadow, and you can also control which parts of the view get shadowed by adjusting your modifier order.

If you want to attach a shadow to other kinds of views, you should use the shadow() modifier instead. In its basic form, you can add a shadow just by specifying the radius of the blur, like this:

Text("Hacking with Swift")
    .foregroundStyle(.black)
    .padding()
    .shadow(radius: 5)
    .border(.red, width: 4)
    .background(.white)

Download this as an Xcode projectopen in new window

The text “Hacking with Swift” in black on a white rectangle with a thick red border. The text has a hazy gray shadow behind it.
The text “Hacking with Swift” in black on a white rectangle with a thick red border. The text has a hazy gray shadow behind it.

That adds a very slight shadow with a 5-point blur centered on the text.

You can also specify which color you want along with the X and Y offset from the original view. For example, this creates a strong red shadow with a 5-point blur, centered on the text:

Text("Hacking with Swift")
    .padding()
    .shadow(color: .red, radius: 5)
    .border(.red, width: 4)

Download this as an Xcode projectopen in new window

The text “Hacking with Swift” with a hazy red shadow behind it.
The text “Hacking with Swift” with a hazy red shadow behind it.

If you want to specify offsets for the shadow, add x and/or y parameters to the modifier, like this:

Text("Hacking with Swift")
    .padding()
    .shadow(color: .red, radius: 5, x: 20, y: 20)
    .border(.red, width: 4)

Download this as an Xcode projectopen in new window

The text “Hacking with Swift” centered in a red rectangular outline. Along the rectangle's bottom edge is a hazy red cloud.
The text “Hacking with Swift” centered in a red rectangular outline. Along the rectangle's bottom edge is a hazy red cloud.

Remember, SwiftUI applies modifiers in the order you list them, so if you want you can have your shadow apply to the border as well just by putting the border modifier before the shadow modifier:

Text("Hacking with Swift")
    .padding()
    .border(.red, width: 4)
    .shadow(color: .red, radius: 5, x: 20, y: 20)

Download this as an Xcode projectopen in new window

The text “Hacking with Swift” centered in a red rectangular outline. Behind and to the bottom right is a blurry shadow of the text and outline.
The text “Hacking with Swift” centered in a red rectangular outline. Behind and to the bottom right is a blurry shadow of the text and outline.

Tips

If you find your shadow effect isn’t strong enough, add another shadow() modifier – you can stack them up to create more complex shadow effects.

Similar solutions…
How to draw a border around a view | SwiftUI by Example

How to draw a border around a view
How to color the padding around a view | SwiftUI by Example

How to color the padding around a view
How to control spacing around individual views using padding | SwiftUI by Example

How to control spacing around individual views using padding
SwiftUI tips and tricks | SwiftUI by Example

SwiftUI tips and tricks
How to draw a border inside a view | SwiftUI by Example

How to draw a border inside a view

이찬희 (MarkiiimarK)
Never Stop Learning.