3

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!

dgund
  • 3,459
  • 4
  • 39
  • 64
sebi
  • 815
  • 7
  • 15
  • 26

2 Answers2

11

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);
Hilton Campbell
  • 6,065
  • 3
  • 47
  • 79
arvind
  • 386
  • 3
  • 12
0

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.

ma11hew28
  • 121,420
  • 116
  • 450
  • 651