7

Is there any way to change the color of a UIButton when it's pressed?

The default seems to be a blue color, but I don't see where or how to change this to a custom color.

Thanks!

rmaddy
  • 314,917
  • 42
  • 532
  • 579
Sandy
  • 2,572
  • 7
  • 40
  • 61
  • That one is referenced from the one I linked. There are several possible duplicates really. Pick one. :) Either way, vote to close as a duplicate. – rmaddy Jun 21 '13 at 05:15
  • I did. And yes you are are right. – Adrian P Jun 21 '13 at 05:22
  • rob you should give resonance of your question's answer given by other SO user.. – Nitin Gohel Jun 21 '13 at 05:31
  • i don't Understand why People not reply after asking Question..? here all given total 7 answer yet but @rob not replay at-list one answer's of his Question.. :@ – Nitin Gohel Jun 24 '13 at 06:02
  • As stated there are several valid answers. I replied in detail below. – Sandy Jun 24 '13 at 06:04
  • Hey, [I made a category that helps you to achieve this](https://github.com/NSElvis/UIButton-ANDYHighlighted). – 3lvis Dec 07 '14 at 14:34
  • See [how to use `setBackgroundImage(...)` to achieve this](https://stackoverflow.com/a/15440165/8740349), without real image! – Top-Master Apr 15 '21 at 15:41

2 Answers2

6

If you are using storyboard then in inspector window you can change the highlight tint property to the color you want on button click event.

highlight tint

Look at the Highlight Tint property in image.

Geek
  • 8,280
  • 17
  • 73
  • 137
  • 31
    This property does not exist in xcode5. – DarkLeafyGreen Mar 25 '14 at 11:55
  • Thanks to this hint given here, I was able to use .tintColor to set the color (as of Xcode 7.1). E.g. let button = UIButton(type: .System) button.title = "My Title" button.tintColor = .redColor() – DonnaLea Nov 09 '15 at 22:04
5

I ultimately followed the following poster's suggestion. It worked perfectly.

https://stackoverflow.com/a/10670141/720175

In my particular case I created a subclass of UIButton, with the final code as:

-(void) setHighlighted:(BOOL)highlighted
{
    if(highlighted) {
        self.backgroundColor = [UIColor colorWithRed:1 green:0.643 blue:0.282 alpha:1];
    } else {
        self.backgroundColor = [UIColor colorWithRed:1 green:1 blue:1 alpha:1];
    }
    [super setHighlighted:highlighted];
}

Easy as pie.

Community
  • 1
  • 1
Sandy
  • 2,572
  • 7
  • 40
  • 61