
- SwiftUI - Home
- SwiftUI - Overview
- SwiftUI vs UIkit
- SwiftUI Environment
- SwiftUI - Environment Setup
- SwiftUI - Basic Components
- SwiftUI - Building First Application
- SwiftUI Views
- SwiftUI - Views
- SwiftUI - Customize Text View
- SwiftUI - Custom Image View
- SwiftUI - Stacks
- SwiftUI Drawing Shapes
- SwiftUI - Shapes
- SwiftUI - Drawing line
- SwiftUI - Drawing Rectangle
- SwiftUI - Drawing Rounded Rectangle
- SwiftUI - Drawing Triangle
- SwiftUI - Drawing Circle
- SwiftUI - Drawing Star
- SwiftUI - Drawing Polygon
- SwiftUI - Drawing Pie chart
- SwiftUI - Using built-in shapes
- SwiftUI - Text
- SwiftUI - Text View
- SwiftUI - Text Input and Output
- SwiftUI - Color
- SwiftUI - Color
- SwiftUI - Colorpicker
- SwiftUI - Gradients
- SwiftUI - Adjust Color
- SwiftUI - Effects
- SwiftUI - Effects
- SwiftUI - Blend Effect
- SwiftUI - BLur Effect
- SwiftUI - Shadow Effect
- SwiftUI - Hover Effect
- SwiftUI - Animations
- SwiftUI - Animations
- SwiftUI - Creating Animations
- SwiftUI - Creating an Explicit Animation
- SwiftUI - Multiple Animations
- SwiftUI - Transitions
- SwiftUI - Asymmetric Transition
- SwiftUI - Custom Transition
- SwiftUI - Image
- SwiftUI - Images
- SwiftUI - Image as Background
- SwiftUI - Rotating Image
- SwiftUI - Media
- SwiftUI - View Layout
- SwiftUI - View Layout
- SwiftUI - View Size
- SwiftUI - View Spacing
- SwiftUI - View Padding
- SwiftUI - UI Controls
- SwiftUI - UI Controls
- SwiftUI - Button
- SwiftUI - CheckBox
- SwiftUI - Menubar
- SwiftUI - Toolbar
- SwiftUI - Search Bar
- SwiftUI - TextField
- SwiftUI - Slider
- SwiftUI - Toggle
- SwiftUI - Pickers
- SwiftUI - Menus
- SwiftUI - List & Tables
- SwiftUI - Lists
- SwiftUI - Static List
- SwiftUI - Dynamic List
- SwiftUI - Customize List
- SwiftUI - Tables
- SwiftUI - Forms
- SwiftUI - Forms
- SwiftUI - Breaking Forms in Sections
- SwiftUI - Event Handling
- SwiftUI - Event Handling
- SwiftUI - Gesture
- SwiftUI - Clipboard
- SwiftUI - Drag and Drop
- SwiftUI - Focus
- SwiftUI - Alert
- SwiftUI - Miscellaneous
- SwiftUI - Containers
- SwiftUI - Navigation
- SwiftUI - Notifications
- SwiftUI - Cross-Platform UI
- SwiftUI - Data
- SwiftUI - Accessibility
- SwiftUI - Framework Integration
- SwiftUI - Framework Integration
- SwiftUI - Interfacing with UIKit
- SwiftUI - Creating macOS App
- SwiftUI Useful Resources
- SwiftUI - Useful Resources
- SwiftUI - Discussion
SwiftUI - Shadow Effect
Shadow effect is used to apply shadow behind the specified view or component inside the view. It is commonly used to provide depth to the view. It is the most commonly used effect in UI development.
It is used to create an illusion that a view is raised above the background view, it makes that view stand out among all the given views. It is generally used with buttons, images, text, etc. to enhance their visual appearance. In SwiftUI, we can create a shadow effect with the help of the .shadow() modifier.
The shadow() Modifier in SwiftUI
SwiftUI provides a pre-defined modifier named shadow() to create a shadow effect on the given view. It can apply a shadow effect to any view such as image view, text view, button, etc.
Using this modifier we can also change the color of the shadow and can also control the opacity of the shadow.
Syntax
Following is the syntax −
func shadow(color:Color, radius:CGFloat, x:CGFloat, y:CGFloat) -> some View
Parameters
Following is the parameters supported by this modifier −
- color: Represent the color of the shadow. By default the color is black.
- radius: Represent the blur of the shadow. If the value of this parameter is high that means more blur in the shadow.
- x: Represent the amount of offset the shadow horizontally from the view.
- y: Represent the amount of offset the shadow vertically from the view.
Example 1
The following SwiftUI program is used to apply the shadow effect on the given shapes.
import SwiftUI struct ContentView: View { var body: some View { VStack{ Rectangle() .fill(.red) .frame(width: 150, height: 160) .shadow(radius: 10) .padding(10) Rectangle() .fill(.red) .frame(width: 150, height: 160) .shadow(radius: 50) .padding(10) Rectangle() .fill(.red) .frame(width: 150, height: 160) .shadow(radius: 0, x: 10.0, y: 10.0) .padding(10) Rectangle() .fill(.red) .frame(width: 150, height: 160) .shadow(radius: 5, x: 20.0, y: 10.0) .padding(10) } } } #Preview { ContentView() }
Output

Example 2
The following SwiftUI program is used to apply the colored shadow effect on the given views.
import SwiftUI struct ContentView: View { var body: some View { VStack{ // Colored Shadow Rectangle() .fill(.red) .frame(width: 150, height: 160) .shadow(color:.green, radius: 10) .padding(10) // Shadow on text Text("Hello Swift") .font(.largeTitle) .shadow(color: .red, radius: 20) // Shadow on image Image("wallpaper") .frame(width: 100, height: 150) .clipShape(Circle()) .shadow(radius: 10) } } } #Preview { ContentView() }
Output

Inner Shadow in SwiftUI
In SwiftUI, we can style shadow with the help of the .inner() modifier. Inner shadow is basically used to creates an illusion that the edges of the view are indented. We can also apply inner shadow with the help of other styling modifiers such as foregroundStyle(), fill(), etc. It can work only with Text, Image and Shape Views.
Example
The following SwiftUI program is used to apply the inner shadow effect on the given view.
import SwiftUI struct ContentView: View { var body: some View { VStack{ Rectangle() .fill(.red.shadow(.inner(radius: 3, x: 6, y: 6))) .frame(width: 150, height: 160) .padding(10) } } } #Preview { ContentView() }
Output

Drop Shadow in SwiftUI
In SwiftUI, we can style shadow with the help of the .drop() modifier. It creates the illusion that the component is lifted above from the background. We can also apply a drop shadow with the help of other styling modifiers such as foregroundStyle(), fill(), etc. It can work only with Text, Image and Shape Views.
Example
The following SwiftUI program is used to apply the drop shadow effect on the given view.
import SwiftUI struct ContentView: View { var body: some View { VStack{ Rectangle() .fill(.red.shadow(.drop(radius: 3, x: 6, y: 6))) .frame(width: 150, height: 160) .padding(10) } } } #Preview { ContentView() }
Output
