Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

UIButton selection color [duplicate]

I am drawing custom a UIButton and want to set the blue color when it is highlighted. How can I achieve this?

UIButton *myButton = [UIButton buttonWithType:UIButtonTypeCustom];
[myButton setBackgroundImage:[[UIImage imageNamed:@"test.png"] stretchableImageWithLeftCapWidth:100 topCapHeight:0] forState:UIControlStateNormal];
like image 449
Abhinav Avatar asked May 20 '11 02:05

Abhinav


2 Answers

You can do following to set images for your state (normal/highlight/selected). You have to have images.

[downButton setImage:[UIImage imageNamed:@"white.png"] forState:UIControlStateNormal];
[downButton setImage:[UIImage imageNamed:@"blue.png"] forState:UIControlStateHighlighted];
like image 143
MobileSoul Avatar answered Oct 02 '22 01:10

MobileSoul


Ya, you can create a 1px by 1px "blue colored" image and use the following:

[yourButton setBackgroundImage:[UIImage imageNamed:@"theImageYouMade.png"] forState:UIControlStateHighlighted];

You can't use "setImage:" in my method because the highlighted state of the button will actually just display the 1px by 1px image in the center of the button.

My method works for variable sized buttons. Using setImage: requires you to make images that are exactly the same size as your button.

-Chris

like image 37
Chris Allinson Avatar answered Oct 01 '22 23:10

Chris Allinson