the_critic
the_critic

Reputation: 12820

UIButton weird shadow behaviour

I am trying to achieve something trivial. I would like to apply a shadow to my UIButton; without a shadow radius. This does not seem to work for me. As soon as I set the shadow radius to 0, the shadow disappears completely, while setting it to any other value will draw the shadow normally.

Here's my code:

 btn = [UIButton buttonWithType:UIButtonTypeCustom];
        btn.frame = CGRectMake(10, someView.frame.size.height-150, someView.frame.size.width-20, 60);
 btn.backgroundColor = UIColorFromRGB(kOrangeColor, 1.0f);
 btn.layer.cornerRadius = kCornerRadius;
 btn.titleLabel.font = kEdmondsansBold(24.0f);
 [btn addTarget:self action:@selector(doSomethingFancy) forControlEvents:UIControlEventTouchUpInside];
 btn.layer.shadowOffset = CGSizeMake(0, 2);
 btn.layer.shadowColor = UIColorFromRGB(0xff2400, 1.0f).CGColor;
 btn.layer.shadowOpacity = 1.0f;
 btn.layer.shadowRadius=  0.0f;
 [btn setTitleColor:UIColorFromRGB(0xffffff, 1.0f) forState:UIControlStateNormal];
 [btn setTitle:@"Fancy fancy" forState:UIControlStateNormal];
 [someView addSubview:btn];

Upvotes: 0

Views: 372

Answers (1)

Kyle Fang
Kyle Fang

Reputation: 1209

When radius is set to 0, as you would expect.
the shadow 'disappears'.

But it didn't, it's just hidden behind the button.

To achieve a solid shadow, you also have to set the offset like this:

button.layer.shadowOffset = CGSizeMake(xOffset, yOffset);

If you were thinking add shadow on all edges, then you should consider using boarder instead of shadow:

button.layer.borderColor = [UIColor blackColor].CGColor;
button.layer.borderWidth = 3.f;

Upvotes: 1

Related Questions