macoscocoasubclassnsprogressindicator

Subclass NSProgressIndicator


i like to subclass a NSProgressIndicator. I've used this code and i set the Subclass in the Interface Builder:

- (void)drawRect:(NSRect)dirtyRect {
NSRect rect = NSInsetRect([self bounds], 1.0, 1.0);
CGFloat radius = rect.size.height / 2;
NSBezierPath *bz = [NSBezierPath bezierPathWithRoundedRect:rect xRadius:radius yRadius:radius];
[bz setLineWidth:2.0];
[[NSColor blackColor] set];
[bz stroke];

rect = NSInsetRect(rect, 2.0, 2.0);
radius = rect.size.height / 2;
bz = [NSBezierPath bezierPathWithRoundedRect:rect xRadius:radius yRadius:radius];
[bz setLineWidth:1.0];
[bz addClip];
rect.size.width = floor(rect.size.width * ([self doubleValue] / [self maxValue]));
NSRectFill(rect);

When the app starts its looks like this: App start

But during the copy progress the old bar shows up. During copying

Whats wrong?


Solution

  • It seems that the progress bar's progress is not drawn in drawRect:, so just overriding drawRect: is not enough. However, if you make the progress bar layer backed, you are responsible for doing all the drawing.

    From the documentation:

    The view class automatically creates a backing layer for you (using makeBackingLayer if overridden), and you must use the view class’s drawing mechanisms.

    Check the "Core Animation Layer" in IB or add this to your sub class:

    - (void)awakeFromNib {
        [super awakeFromNib];
        [self setWantsLayer:YES];
    }