Search code examples
objective-cuitableviewuibuttonglow

How to change UIButton's background image while clicking it?


I have a UIButton in each of my tableview's cells. I want to glow the uibutton while click action happends. I set an image as its background image using the code

UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];

button.frame = CGRectMake(270,10,30,30);

[button setImage:[UIImage imageNamed:@"image.png"] forState:UIControlStateNormal];

[button addTarget:self action:@selector(action:) forControlEvents:UIControlEventTouchUpInside];

button.backgroundColor = [UIColor clearColor];

The image.png is little bit darker image. Because i want to show a click effect by showing a glowed image.

Now shall i glow the image.png by code while clicking happends? Or should i create a new glowing image(say image_glow.png) with the same dimension and replace the previous one?

Whatever, please explain how to implement this....


Solution

  • The best way is to use multiple images.

    You can set different image to different state of a UIButton by using setImage method.

    As i see, you have set image only for UIControlStateNormal state, which makes all other states of a button to use the same image.

    reference : UIButton States

    Hope this helps.