What's the best way to add a drop shadow to my UIView
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.
Introduction
The best way to add a shadow to a UIView is usually to configure its backing layer directly and be explicit about shape and performance. A shadow can look correct in a few lines, but getting it to behave well with rounded corners, scrolling, and Auto Layout takes a bit more care.
Use the View's Layer, Not a Custom Drawing Hack
Every UIView has a CALayer, and shadows are layer properties. For most cases you do not need custom drawing or extra image assets.
These properties control the visual result:
- '
shadowColorsets the color' - '
shadowOpacitycontrols visibility' - '
shadowOffsetcontrols direction and distance' - '
shadowRadiuscontrols blur'
This is the right starting point, but it is not the whole story.
Add a shadowPath for Performance
Without a shadow path, Core Animation may need to calculate the shadow shape dynamically. That is slower than giving the system the exact outline.
Setting the path inside layoutSubviews is important because the view's size may change after Auto Layout runs.
Rounded Corners and Shadows Need Separate Responsibilities
One of the most common problems is combining rounded corners and clipping on the same layer that draws the shadow. If masksToBounds is true, the shadow gets clipped away.
The usual fix is to use two views:
- an outer container that owns the shadow
- an inner content view that clips to rounded corners
This pattern is more reliable than trying to make one layer do both jobs.
Tune the Shadow to Match the Design
Most shadows look bad because the opacity is too high or the offset is too dramatic. A modern iOS shadow usually works better with:
- low opacity
- moderate blur
- vertical offset greater than horizontal offset
Good defaults for a card-like component are often:
- opacity between
0.10and0.22 - radius between
8and20 - offset around
0, 4to0, 10
Use the smallest shadow that still communicates depth. Heavy shadows can make the interface look muddy.
When Rasterization Helps
If a complex shadowed view is animating or appears in a scrolling list, rasterization can help in some cases. Use it carefully, because it trades CPU work for cached bitmap memory.
Only do this after profiling. It is not a default recommendation for every shadowed view.
Common Pitfalls
The most common mistake is enabling masksToBounds on the same layer that should display the shadow. That clips the shadow completely.
Another issue is forgetting to update shadowPath when the view resizes. Shadows then look wrong or performance drops because Core Animation must infer the shape repeatedly.
A third problem is overdesigning the effect. Large blur radii and dark shadows look artificial and can make text or controls feel less sharp.
Summary
- Add shadows through the view's layer, not through custom image tricks.
- Set a
shadowPathwhenever the shape is known. - Use a container view for the shadow and an inner view for rounded clipped content.
- Keep opacity and blur restrained for a cleaner iOS look.
- Profile before enabling rasterization or other performance tweaks.
Related reading
- What's the best way to detect when the app is entering the background for my view?
- What's the best way to develop a sideswipe menu like the one in Facebook's new iOS app?
- What's the best way to iterate an Android Cursor?
- What's the best way to limit text length of EditText in Android
- What's the best way to limit text length of EditText in Android
- What's the best way to share data between activities?
- What's the best way to share data between activities?
- What's the cleanest way of applying map to a dictionary in Swift?
.png&w=3840&q=75)
Tackling System Design Interview Problems
A short course that equips you with the skills to approach system design interviews methodically.
Start the free courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.