简体   繁体   中英

Displaying NSProgressIndicatorSpinningStyle in NSStatusItem and then hiding it

I'm working on NSStatusItem. I've managed to use the setImage and setAlternateImage to work. When user selects something, it takes a while for it to accomplish whatever it is doing. While it is doing something, I tried changing from the usual Image to a spinner. The way that I am doing it now is that I created a view, set the NSProgressIndicator to it, and then use

[statusItem setView: viewWithSpinner];

It seems to work until I try to remove it and display the original image. The only way I can hide it is to do

[statusItem setView: nil];

but that breaks everything, the original images don't come back. I guess cause there's no more view. I can't seem to save the original view before setting the viewWithSpinner.

Can someone advise me of a way of accomplishing this?

So...

NSStatusItem *myStatusItem;

- (void)applicationDidFinishLaunching:(NSNotification *)aNotification
{
[myStatusItem setImage:statusImage];
[myStatusItem setHighlightMode:YES];
[myStatusItem setAlternateImage:statusImageSel];
[myStatusItem setMenu:myStatusMenu];
etc...
[self createSpinner];
}

-(void)createSpinner
{
//to overcome the white border problem

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];

//for testing purposes
[[myStatusItem view] addSubview:progressIndicatorHolder];

spinnerView = progressIndicatorHolder;
}

I suggest save old view using [statusItem view] before setting to any other view. Before coming back to normal menu, set old saved view to statusItem using setView method.

如果您只想隐藏NSStatusItem视图,只需调用[yourStatusItem setLength:0]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM