Kevin Science Kevin Science - 1 year ago 168
Objective-C Question

How can I add a drop shadow effect beneath image views on iOS?

I want to add a drop shadow effect underneath my

instances, like in the screenshot below:

enter image description here

How can I achieve this, either through storyboards or code (Objective-C)?

Answer Source

Try this:

UIBezierPath *shadowPath = [UIBezierPath bezierPathWithRect:view.bounds];
view.layer.masksToBounds = NO;
view.layer.shadowColor = [UIColor blackColor].CGColor;
view.layer.shadowOffset = CGSizeMake(0.0f, 5.0f);
view.layer.shadowOpacity = 0.5f;
view.layer.shadowPath = shadowPath.CGPath;

Explaination: As seen in the last step there are a few different properties needed when adding a shadow using Core Animation. If your are not familiar with Core Animation don’t worry, just try to focus on the shadow code and not the CALayer on the view.

shadowOffset is a CGSize representing how far to offset the shadow from the path.

shadowColor is the color of the shadow. Shadow colors should always be opaque, because the opacity will be set by the shadowOpacity property. The shadowColor property is a CGColor not a UIColor.

shadowRadius is the width of the shadow along the shadow path

shadowOpacity determines the opacity of the shadow.

shadowPath is probably the most important of the properties. While a shadow can be drawn without specifying a path, for performance reasons you should always specify one. This path tells Core Animation what the opaque regions of the view are, and without it, things slow down severely! It is a CGPath, which is most easily created using UIBezierPath (iOS only) as shown in step 2.

See more in

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download