Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to add a drop shadow to a UIButton?

I would like to add a drop shadow to a UIButton. I tried to use self.layer.shadow* properties. Those properties work in UIView, but they behave differently in UIButton. I would really appreciate it if I could get any pointers to draw the drop shadow. Thank you!

self.layer.cornerRadius = 8.0f; self.layer.masksToBounds = YES; self.layer.borderWidth = 1.0f;  self.layer.shadowColor = [UIColor greenColor].CGColor; self.layer.shadowOpacity = 0.8; self.layer.shadowRadius = 12; self.layer.shadowOffset = CGSizeMake(12.0f, 12.0f); 
like image 890
Chris Frost Avatar asked Feb 23 '10 00:02

Chris Frost


People also ask

How to add shadow in iOS?

iOS has built-in support for adding shadows to UIView components. All you need to do in order to add a shadow to a UIView object is to set the shadow properties of the view's layer. Shadows are a powerful user interface design element, making the visual components stand out by giving them a 3D look and feel.

How to add shadow in storyboard xcode?

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.


Video Answer


1 Answers

There is only one tiny mistake in the question that causes the shadow to not be displayed: masksToBounds:YES also masks the shadow! Here's the correct code:

self.layer.cornerRadius = 8.0f; self.layer.masksToBounds = NO; self.layer.borderWidth = 1.0f;  self.layer.shadowColor = [UIColor greenColor].CGColor; self.layer.shadowOpacity = 0.8; self.layer.shadowRadius = 12; self.layer.shadowOffset = CGSizeMake(12.0f, 12.0f); 

Unfortunately, this means we cannot mask the content and have a shadow at the same time without tricks.

Remember to #import <QuartzCore/QuartzCore.h>.

like image 113
fzwo Avatar answered Sep 28 '22 03:09

fzwo