Question

I'm using this code (inspired by an other question on here) :

- (void)showProgressIndicator {

    if (statusItem) {

        NSLog(@"wassup");
        NSView *progressIndicatorHolder = [[NSView alloc] init];
        NSProgressIndicator *progressIndicator = [[NSProgressIndicator alloc] init];
        [progressIndicator setBezeled: NO];
        [progressIndicator setStyle: NSProgressIndicatorSpinningStyle];
        [progressIndicator setControlSize: NSSmallControlSize];
        [progressIndicator sizeToFit];
        [progressIndicator setUsesThreadedAnimation:YES];
        [progressIndicatorHolder addSubview:progressIndicator];
        [progressIndicator startAnimation:self];
        [statusItem setView:progressIndicatorHolder];
        [progressIndicator setNextResponder:progressIndicatorHolder];
        [progressIndicatorHolder setNextResponder:statusItem];
    }
}

Unfortunately, as soon as this code runs the status item (which is initially showing an image) disappears... Why doesn't my code work?

Était-ce utile?

La solution

You probably need to explicitly set the frame on progressIndicatorHolder then center progressIndicator within its superview, e.g.:

CGRect holderRect = progressIndicatorHolder.bounds;
CGRect indicatorRect = progressIndicatorHolder.frame;
indicatorRect.origin.x = (holderRect.size.width - indicatorRect.size.width)/2.0f;
indicatorRect.origin.y = (holderRect.size.height - indicatorRect.size.height)/2.0f;
progressIndicator.frame = indicatorRect;

As an alternative, if you find that you want to do more sophisticated layout, you could load the NSStatusItem's view from a nib.

Autres conseils

Following code is working for me.

progressIndicator = [[NSProgressIndicator alloc] init];

[progressIndicator setBezeled: YES];
[progressIndicator setStyle: NSProgressIndicatorSpinningStyle];
[progressIndicator setControlSize: NSSmallControlSize];
[progressIndicator sizeToFit];
[progressIndicator setUsesThreadedAnimation:YES];

oldView = [statusItem view];
[statusItem setView:progressIndicator];

[progressIndicator sizeToFit];
[statusItem setView:progressIndicator];
[progressIndicator startAnimation:self];

Please note progressIndicatorHolder is not being set anywhere.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top