Kevin Science
Kevin Science

Reputation: 303

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

I want to add a drop shadow effect underneath my UIImageView instances, like in the screenshot below:

enter image description here

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

Upvotes: 1

Views: 2760

Answers (3)

Saraswati
Saraswati

Reputation: 1526

//Drop Shadow
[view.layer setShadowColor: [UIColor grayColor].CGColor];
[view.layer setShadowOpacity:0.8];
[view.layer setShadowRadius:3.0];
[view.layer setShadowOffset:CGSizeMake(2.0, 2.0)];

Upvotes: 4

Proton
Proton

Reputation: 1365

Please Try this once :-

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;

Explanation :- 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 http://nscookbook.com/2013/01/ios-programming-recipe-10-adding-a-shadow-to-uiview/

Upvotes: 2

Arasuvel
Arasuvel

Reputation: 3012

Those are not lights, but shadow. You can use the layer to draw shadow for UI elements.

Upvotes: 0

Related Questions