我正在使用这段代码(灵感来自这里的另一个问题):
- (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];
}
}
不幸的是,一旦此代码运行,状态项(最初显示图像)就会消失......为什么我的代码不起作用?
您可能需要在其超级视图中明确设置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
的视图。
以下代码对我有用。
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];
请注意,进度指示器持有人未在任何地方设置。