1

I'm trying to subclass ProgressView to use as a health bar for a game and when I try to override the setProgress, it isn't called.

I want the color to change based on the progress float value.

Here is the code I tried to use:

@interface AHHealthbar : UIProgressView
...
- (void)setProgress:(float)progress animated:(BOOL)animated
{

if (progress > .5)
{
    self.progressTintColor = [UIColor greenColor];
}

if (progress <= .5 && self.progress >= .2)
{
    self.progressTintColor = [UIColor yellowColor];
}

if (progress < .2) {
    self.progressTintColor = [UIColor redColor];

}

[super setProgress:progress animated:animated];
}

Does any one have an idea of how to override this? Or any better ideas?

Ahufford
  • 488
  • 2
  • 12
  • Are you sure you properly initiate it as AHHealthbar (not UIProgressView) it's easy to forget this, especially when initiated from a NIB file. Check your variable pointer type in the debugger. – demosten Dec 23 '12 at 23:32
  • I can confirm that AHHealthbar is being instantiated, but my setProgress method is never called. I made an override of drawrect that was called. But I don't want to do the drawing myself, just the colors. – Ahufford Dec 24 '12 at 00:15
  • Well, did you try overriding `-setProgress:`? – StilesCrisis Dec 24 '12 at 00:42
  • That is exactly what I am trying to override.... – Ahufford Dec 24 '12 at 17:13

1 Answers1

1

You might need to also override -setProgress:.

StilesCrisis
  • 15,972
  • 4
  • 39
  • 62