0

I am trying to change the color and image of a button when its pressed. This is what i have

 - (IBAction)likeButtonTouched:(UIButton*)sender {
sender.imageView.image = [sender.imageView.image imageWithRenderingMode:UIImageRenderingModeAlwaysTemplate];
[sender setImage: [UIImage imageNamed:@"Like Filled"] forState:UIControlStateNormal];
[sender setTintColor:secondaryColor];
}

This code works in viewDidLoad but in this, the tint color isnt changed but the image is. Anyone know why?

Mark Bourke
  • 9,806
  • 7
  • 26
  • 30
  • You can try to take a look on this link http://stackoverflow.com/questions/14523348/how-to-change-the-background-color-of-a-uibutton-while-its-highlighted – Siti Oct 30 '15 at 13:19
  • Silly question but are you sure that `secondaryColor` has a valid, non-nil value? – rmaddy Oct 30 '15 at 17:41

4 Answers4

1

You can Override the UIButton's method with the following Method. Just change the background color of the button when it is in highlighted state.

- (void)setHighlighted:(BOOL)highlighted {
      [super setHighlighted:highlighted];

      if (highlighted) {
          self.backgroundColor = [UIColor redColor]; // whatever color you want
     }
}
nilam_mande
  • 931
  • 7
  • 14
0

I have written a subclass of a UIButton that does something similar a while ago:

@implementation OnTouchHighlightButton

- (void)awakeFromNib
{
    [super awakeFromNib];

    [self addActionSelectors];
}

- (void)addActionSelectors
{
    [self addTarget:self action:@selector(onTouchDown) forControlEvents:(UIControlEventTouchDown | UIControlEventTouchDragEnter)];
    [self addTarget:self action:@selector(onTouchUp) forControlEvents:(UIControlEventTouchUpInside | UIControlEventTouchUpOutside | UIControlEventTouchDragExit | UIControlEventTouchCancel)];
}

- (void)onTouchDown
{
    [UIView transitionWithView:self
                      duration:0.15
                       options:UIViewAnimationOptionTransitionCrossDissolve
                    animations:^{
                        self.highlighted = YES;
                    }
                    completion:nil];
}

- (void)onTouchUp
{
    [UIView transitionWithView:self
                      duration:0.15
                       options:UIViewAnimationOptionTransitionCrossDissolve
                    animations:^{
                        self.highlighted = NO;
                    }
                    completion:nil];
}

@end

You can add whatever you want on the onTouchUp and onTouchDown methods. You can also change the control events that fire these methods.

Snacks
  • 513
  • 4
  • 22
0

Unfortunately, Apple does not provide forState: setter methods for the tintColor and backgroundColor properties on UIButton, so you will have to use control events. However, you shouldn't have to subclass UIButton.

- (void)viewDidLoad {
    [super viewDidLoad];

    [self.button addTarget:self action:@selector(changeButtonToAppearHightlighted:) forControlEvents:UIControlEventTouchDown | UIControlEventTouchDragInside];
    [self.button addTarget:self action:@selector(changeButtonToAppearUnhightlighted:) forControlEvents:UIControlEventTouchDragOutside | UIControlEventTouchCancel | UIControlEventTouchUpOutside | UIControlEventTouchUpInside];
}

- (void)changeButtonToAppearHightlighted:(UIButton *)sender {
    if ([sender isEqual:self.button]) {
        sender.tintColor = BUTTON_TINTCOLOR_HIGHLIGHTED;
        sender.backgroundColor = BUTTON_BACKGROUNDCOLOR_HIGHLIGHTED;
    }
}

- (void)changeButtonToAppearUnhightlighted:(UIButton *)sender {
    if ([sender isEqual:self.button]) {
        sender.tintColor = BUTTON_TINTCOLOR_UNHIGHLIGHTED;
        sender.backgroundColor = BUTTON_BACKGROUNDCOLOR_UNHIGHLIGHTED;
    }
}
n00neimp0rtant
  • 905
  • 1
  • 8
  • 18
  • @MarkBourke doesn't compile? unexpected behavior? nothing happens at all? Please elaborate – n00neimp0rtant Oct 30 '15 at 13:35
  • sorry, it complies but the color doesnt change – Mark Bourke Oct 30 '15 at 13:37
  • what i want to do is, change the color and the image when the button is "selected" i dont want to change the highlight state – Mark Bourke Oct 30 '15 at 13:38
  • @MarkBourke so this is supposed to act like a toggle or on/off button? If that's the case, you may want to revise your question. "When it's pressed" is somewhat vague. – n00neimp0rtant Oct 30 '15 at 13:43
  • i dont see how changing the question will have anything to do with that. i want to change the color of a uibutton image – Mark Bourke Oct 30 '15 at 19:50
  • "when its pressed" could mean "when you first touch down on the button", "when you release your touch off of the button", "when the button is tapped once" (if it is a toggle button)... – n00neimp0rtant Nov 02 '15 at 15:31
-3

Sorry for last time reply; Some get wrong info.

Here I am updating the answer in Swift 3.0 ; Hope it works well as per your query;

@IBOutlet weak var colorChangeBtn: UIButton!


override func viewDidLoad() {
    super.viewDidLoad()
    colorChangeBtn.backgroundColor = UIColor.blue
    colorChangeBtn.tintColor = UIColor.cyan
    colorChangeBtn.setImage(#imageLiteral(resourceName: "select.png"), for: UIControlState.normal)
  }

@IBAction func colorChange(_ sender: UIButton) {
    sender.backgroundColor = UIColor.red
    sender.tintColor = UIColor.blue
    sender.setImage(#imageLiteral(resourceName: "focus.png"), for: .normal)
  }