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];
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];
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With