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!
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.
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) .
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.
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.
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
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)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With