Custom UIProgressView drawing weirdness

Posted by Werner on Stack Overflow See other posts from Stack Overflow or by Werner
Published on 2012-07-10T14:45:08Z Indexed on 2012/07/10 15:15 UTC
Read the original article Hit count: 208

Filed under:
|
|
|

I am trying to create my own custom UIProgressView by subclassing it and then overwrite the drawRect function. Everything works as expected except the progress filling bar. I can't get the height and image right.

The images are both in Retina resolution and the Simulator is in Retina mode. The images are called: "[email protected]" (28px high) and "[email protected]" (32px high).

CustomProgressView.h

#import <UIKit/UIKit.h>

@interface CustomProgressView : UIProgressView

@end

CustomProgressView.m

#import "CustomProgressView.h"

@implementation CustomProgressView

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        // Initialization code
    }
    return self;
}


// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect
{
    // Drawing code
    self.frame = CGRectMake(self.frame.origin.x, self.frame.origin.y, self.frame.size.width, 16);

    UIImage *progressBarTrack = [[UIImage imageNamed:@"progressBarTrack"] resizableImageWithCapInsets:UIEdgeInsetsZero];
    UIImage *progressBar = [[UIImage imageNamed:@"progressBar"] resizableImageWithCapInsets:UIEdgeInsetsMake(4, 4, 5, 4)];

    [progressBarTrack drawInRect:rect];

    NSInteger maximumWidth = rect.size.width - 2;
    NSInteger currentWidth = floor([self progress] * maximumWidth);

    CGRect fillRect = CGRectMake(rect.origin.x + 1, rect.origin.y + 1, currentWidth, 14);

    [progressBar drawInRect:fillRect];
}

@end

The resulting ProgressView has the right height and width. It also fills at the right percentage (currently set at 80%). But the progress fill image isn't drawn correctly.

Does anyone see where I go wrong?

Screenshot to show the problem

© Stack Overflow or respective owner

Related posts about objective-c

Related posts about ios