在NSStatusItem中使用NSProgressIndicator

时间:2012-09-22 10:56:35

标签: objective-c macos cocoa nsstatusitem nsprogressindicator

我正在使用此代码(受到此处其他问题的启发):

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

不幸的是,只要此代码运行状态项(最初显示图像)就会消失...为什么我的代码不起作用?

2 个答案:

答案 0 :(得分:3)

您可能需要在frame上明确设置progressIndicatorHolder,然后在其超级视图中设置progressIndicator,例如:

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;

作为替代方案,如果您发现要进行更复杂的布局,可以从笔尖加载NSStatusItem的视图。

答案 1 :(得分:0)

以下代码对我有用。

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

请注意,progressIndicatorHolder未在任何地方设置。