Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

simplest (rectangular) drop shadow for an UIView

I've seen lots of snippets that either: are too complicated for something as simple as a drop shadow, requiring subclassing UIView and using quartz2d calls, or I can't get them to work.

I just want to do this on a view I'm adding as a subview to another one (the subview is taken from another viewController I'm just allocating - I know that's probably not nice but oh well), no IB or anything. what's the simplest / most accepted way to go about it? would it be different if I want it to work on iOS 4?

like image 291
maltalef Avatar asked Jul 25 '12 02:07

maltalef


People also ask

How do you add a shadow to a storyboard?

Go to the Storyboard. Add a Button to the main view and give it a title of "Shadow Tutorial". Select the Resolve Auto Layout Issues button and select Reset to Suggested Constraints. The Storyboard should look like this.

What is shadow offset Swift?

shadowOffset controls how far the shadow is moved away from its view. This defaults to 3 points up from the view. shadowOpacity controls how transparent the shadow is. This defaults to 0, meaning “invisible”. shadowPath controls the shape of the shadow.


1 Answers

It's as easy as importing <QuartzCore/QuartzCore.h> and using a similar snippet as below:

self.viewAboutContainer.layer.shadowColor = [[UIColor blackColor] CGColor];
self.viewAboutContainer.layer.shadowOpacity = 0.7;
self.viewAboutContainer.layer.shadowRadius = 4.0;
self.viewAboutContainer.layer.shadowOffset = CGSizeMake(5.0f, 5.0f);    
self.viewAboutContainer.layer.shadowPath = [UIBezierPath bezierPathWithRect:self.viewAboutContainer.bounds].CGPath;
like image 79
Brayden Avatar answered Oct 17 '22 08:10

Brayden