3

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

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

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

4

2 回答 2

3

您可能需要在其超级视图中显式设置frameon progressIndicatorHolderthen center 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从 nib 加载 的视图。

于 2012-09-23T10:48:50.770 回答
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 没有在任何地方设置。

于 2013-03-11T20:35:20.087 回答