【问题标题】:Using an NSProgressIndicator in an NSStatusItem在 NSStatusItem 中使用 NSProgressIndicator
【发布时间】:2012-09-22 10:56:35
【问题描述】:

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

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

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

【问题讨论】:

  • (1) 您使用的是 ARC,对吗? (2) 视图progressIndicatorHolder 的大小是多少?如果您明确设置progressIndicatorHolderframe,这有帮助吗?
  • (1) 是 (2) 我没有指定尺寸,但显然它的宽度是 0。现在要尝试几件事...
  • 所以我明确设置了progressIndicatorHolderframe,它工作正常,但由于某种原因进度指示器没有居中(它太低)。我将如何离开这里?像这样显式分配frame 可以吗?
  • 您可能需要manually center。作为替代方案,如果您想做更复杂的布局,您可以将自定义视图放在一个 nib 中,并在您为 NSStatusItem 创建视图时按需加载它。
  • 好的,谢谢艾伦!如果您想让您的 cmets 成为答案,我会选择它作为最佳答案,因为它解决了我的问题。

标签: objective-c macos cocoa nsstatusitem nsprogressindicator


【解决方案1】:

您可能需要在progressIndicatorHolder 上显式设置frame,然后在其父视图中居中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;

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

【讨论】:

    【解决方案2】:

    以下代码对我有用。

    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 没有在任何地方设置。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-08-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多