Prateek Chaubey
Prateek Chaubey

Reputation: 645

Add border and shadow to the buttons

I want to add medium thick black border and shadow to the buttons that I have added in my iPad application. I have taken those buttons as custom as I have applied images on them. How can I add border and shadow to ht

Upvotes: 9

Views: 31800

Answers (2)

Arslan
Arslan

Reputation: 919

To add shadows and border is simple.

1) Add the QuartzCore framework to your target.
2) Import the framework header in the class where you want to add borders and shadows. (Or if you have custom class for the button then you can simple import this framework in that class.)
3) To add the border to the button use this code (where button is an IBOutlet connected with the button in interface):

[self.button.layer setBorderWidth:3.0];
[self.button.layer setBorderColor:[[UIColor blackColor] CGColor]];


4) To add the shadow to the button use the following code:

[self.button.layer setShadowOffset:CGSizeMake(5, 5)];
[self.button.layer setShadowColor:[[UIColor blackColor] CGColor]];
[self.button.layer setShadowOpacity:0.5];

You can play around with the values and see how it will affect the behaviour.

Upvotes: 55

Wienke
Wienke

Reputation: 3733

If you were in Cocoa, you could use NSView's setShadow. (NSView is an ancestor of NSButton.) I don't see the equivalent method for UIView, so I don't think there is any simple way of doing that in iOS. Come to think of it, I don't think I've seen that effect in iOS apps, period.

But since you're using custom button images anyway, why not prepare images that include shadow and border?

Upvotes: 0

Related Questions