Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Set Shadow on Bottom UIView only

Tags:

ios

uiview

shadow

I want to create bottom only shadow on UIView. Right now with this function, will create shadow in top, bottom, left, and right.

func setCardView(view : UIView){     view.layer.masksToBounds = false     view.layer.shadowOffset = CGSize(width: 0, height: 0)     view.layer.shadowRadius = 2     view.layer.shadowOpacity = 0.5 } 

Is there anyway to only create shadow in the bottom? Any help would be appreciated. Thank you!

like image 549
Sonic Master Avatar asked Mar 30 '17 03:03

Sonic Master


People also ask

How do you add a shadow to the bottom in UIView?

You can't really just create a shadow for one side. With that being said, the shadow will always be the shadow of the view as a whole. However, you can change the shadow offset to make it towards to the bottom. This means you want the light source shoot the light from top to make the shadow to the bottom.

What is shadow offset?

shadowOffset : the offset of the layer's shadow. The type of this property is CGSize. Width controls the shadow's horizontal offset, and the height controls its vertical offset. The default value of this property is (0.0, -3.0) .

What is shadowPath?

shadowPath controls the shape of the shadow. This defaults to nil , which causes UIKit to render the view offscreen to figure out the shadow shape. shadowRadius controls how blurred the shadow is. This defaults to 3 points.

What is shadow opacity in Swift?

shadowOpacity sets how transparent the shadow is, where 0 is invisible and 1 is as strong as possible. shadowOffset sets how far away from the view the shadow should be, to give a 3D offset effect.


2 Answers

I think the proper way of thinking of shadow is, the shadow belongs to the object, which is the button, the uiview, not just part of the side. Imagining there is a virtual light source. You can't really just create a shadow for one side.

With that being said, the shadow will always be the shadow of the view as a whole. However, you can change the shadow offset to make it towards to the bottom.

view.layer.shadowOffset = CGSize(width: 0, height: 3) 

This means you want the light source shoot the light from top to make the shadow to the bottom. The reason you still see some shadow at the top is the shadow radius. which is to simulate the diffuse of the light. The more diffuse the light is, the softer the shadow will be so you will see top shadow still.

view.layer.shadowRadius = 1 or 0.5 

try to reduce the radius also. it will give you a better visual result.

To understand umbra, penumbra and antumbra if you need, check out https://en.wikipedia.org/wiki/Umbra,_penumbra_and_antumbra

like image 82
Elvin Avatar answered Sep 19 '22 18:09

Elvin


This code working for swift 4 and shadow applying for view Bottom:

view.layer.masksToBounds = false view.layer.shadowRadius = 4 view.layer.shadowOpacity = 1 view.layer.shadowColor = UIColor.gray.cgColor view.layer.shadowOffset = CGSize(width: 0 , height:2) 
like image 27
Srinivasan.M Avatar answered Sep 22 '22 18:09

Srinivasan.M