sebi
sebi

Reputation: 837

Add background shadow to UIButton

How can add a light gray shadow to a UIButton, I don't want a method to do this at the moment, it should be something like:

UIButton *button1... button1.layer.shadowOpacity = 0.8

etc, but that doesn't work, it only adds a shadow inside the button, but I need it on the outside. Thanks!

Upvotes: 3

Views: 6910

Answers (2)

arvind
arvind

Reputation: 386

First you have to #import <QuartzCore/QuartzCore.h>. Then:

mybtn.layer.shadowColor = [UIColor blackColor].CGColor;
mybtn.layer.shadowOpacity = 0.5;
mybtn.layer.shadowRadius = 2;
mybtn.layer.shadowOffset = CGSizeMake(3.0f,3.0f);

Upvotes: 11

ma11hew28
ma11hew28

Reputation: 126327

You can also use –[UIButton setBackgroundImage:forState:] to set the background image for UIControlStateNormal to one with a shadow. E.g.:

[button setBackgroundImage:[UIImage imageNamed:@"ButtonBackgroundNormal"]
                 forState:UIControlStateNormal];

where ButtonBackgroundNormal.png has a shadow. Images often render faster than drawing with code. And, speed is important, especially if you're adding it to a UITableViewCell. In that case, to speed up scrolling speed, make sure the background image is completely opaque by designing it with the same background color of the UITableViewCell and saving it without transparency. Then, set button.opaque = YES.

Upvotes: 0

Related Questions