UIButton selection color

22,654

Solution 1

You can try to use another image to do the job. You can also refer to this link:

Is it even possible to change a UIButtons background color?

Solution 2

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

Solution 3

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];
Share:
22,654
Abhinav
Author by

Abhinav

Updated on March 16, 2020

Comments

  • Abhinav
    Abhinav about 4 years

    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];
    
  • jscs
    jscs almost 13 years
    If another SO question answers this one so completely that all you have to do is link to it, please flag to close as a duplicate rather than posting.
  • len
    len about 11 years
    It is not a duplicate. The linked question does not address the highlight color, only the background color.
  • Top-Master
    Top-Master about 3 years
    And if the color is dynamic see how to create image from color