1

I am creating a custom catagory class for UIButton but it doesn't have the shadow functionality in it. So how should I add shadow on the button when that button is clicked.

Sam Hosseini
  • 813
  • 2
  • 9
  • 17
Vinay Kumar
  • 3,257
  • 1
  • 20
  • 19

2 Answers2

0

If you need highlight on your button than just add:

button.highlighted= YES;

and if you want shadow of hover type effect than just add another image to ur button and set it controlstatenormal to hightlighted that is :

//for normal tap
[button setBackgroundImage:[UIImage imageNamed:@"normal.png"] forState:UIControlStateNormal];

//for hovering effect
[button setBackgroundImage:[UIImage imageNamed:@"hover.png"] forState:UIControlStateHighlighted];
Abha
  • 1,032
  • 1
  • 13
  • 36
  • button.highlighted sets it to higlighted state from the very beginning, but I just wanted to know that if there is any inbuilt property of UIButton by which we can add shadow to the custom button without relying on the background images. – Vinay Kumar Mar 08 '13 at 06:54
  • http://stackoverflow.com/questions/11169225/add-background-shadow-to-uibutton may this link help u – Abha Mar 08 '13 at 07:03
0

Every button has few states :

  enum {
   UIControlStateNormal               = 0,
   UIControlStateHighlighted          = 1 << 0,
   UIControlStateDisabled             = 1 << 1,
   UIControlStateSelected             = 1 << 2,
   UIControlStateApplication          = 0x00FF0000,
   UIControlStateReserved             = 0xFF000000
  };

You can set a different image for each state with:

- (void)setBackgroundImage:(UIImage *)image forState:(UIControlState)state

If you want to set only the title shadow, you can use:

- (void)setTitleShadowColor:(UIColor *)color forState:(UIControlState)state

BTW

If you are using IB you can set the state images in the interface.

shannoga
  • 19,649
  • 20
  • 104
  • 169
  • I tried to change the title shadow color but there was no change in the shadow effect of the UIButton... I want the shadow to be all over the button. – Vinay Kumar Mar 08 '13 at 06:58
  • I you want to change under all the button you should set a different image to the highlighted state. and then set the button state to highlighted. see @Aaaaa answer – shannoga Mar 08 '13 at 07:01